1+ <?php
2+ // include defines for access to global variables
3+ include '../defines.php ' ;
4+
5+ // syntax for api call endpoint
6+ $ endpointSyntax = 'GET https://graph.facebook.com/v11.0/{ig-user-id}/stories ' ;
7+
8+ /**
9+ * Get a users stories
10+ *
11+ * @param string $instagramAccountId
12+ * @param string $accessToken
13+ *
14+ * @return array with the api response
15+ */
16+ function getUserStories ( $ instagramAccountId , $ accessToken ) {
17+ // actual endpoint with a media
18+ $ endpoint = 'https://graph.facebook.com/v11.0/ ' . $ instagramAccountId . '/stories ' ;
19+
20+ $ params = array ( // parameters for the endpoint
21+ 'access_token ' => $ accessToken
22+ );
23+
24+ // make the api call and get a response
25+ $ response = makeApiCall ( $ endpoint , 'GET ' , $ params );
26+
27+ // return data from the response
28+ return $ response ['data ' ]['data ' ];
29+ }
30+
31+ /**
32+ * Get media info
33+ *
34+ * @param array $media
35+ * @param string $accessToken
36+ *
37+ * @return array with the api response
38+ */
39+ function getMediaInfo ( $ media , $ accessToken ) {
40+ // actual endpoint with a media
41+ $ endpoint = 'https://graph.facebook.com/v11.0/ ' . $ media ['id ' ];
42+
43+ $ params = array ( // parameters for the endpoint
44+ 'fields ' => 'caption,id,ig_id,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username ' ,
45+ 'access_token ' => $ accessToken
46+ );
47+
48+ // make the api call and get a response
49+ $ response = makeApiCall ( $ endpoint , 'GET ' , $ params );
50+
51+ // return data from the response
52+ return $ response ['data ' ];
53+ }
54+
55+ /**
56+ * Make a a curl call to an endpoint with params
57+ *
58+ * @param string $endpoint we are hitting
59+ * @param string $type of request
60+ * @param array $params to send along with the request
61+ *
62+ * @return array with the api response
63+ */
64+ function makeApiCall ( $ endpoint , $ type , $ params ) {
65+ // initialize curl
66+ $ ch = curl_init ();
67+
68+ // create endpoint with params
69+ $ apiEndpoint = $ endpoint . '? ' . http_build_query ( $ params );
70+
71+ // set other curl options
72+ curl_setopt ( $ ch , CURLOPT_URL , $ apiEndpoint );
73+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
74+
75+ // get response
76+ $ response = curl_exec ( $ ch );
77+
78+ // close curl
79+ curl_close ( $ ch );
80+
81+ return array ( // return data
82+ 'type ' => $ type ,
83+ 'endpoint ' => $ endpoint ,
84+ 'params ' => $ params ,
85+ 'api_endpoint ' => $ apiEndpoint ,
86+ 'data ' => json_decode ( $ response , true )
87+ );
88+ }
89+
90+ // get the users stories
91+ $ stories = getUserStories ( $ instagramAccountId , $ accessToken );
92+
93+ foreach ( $ stories as &$ story ) { // loop over the each story element
94+ // add the story media info to the story
95+ $ story ['media_info ' ] = getMediaInfo ( $ story , $ accessToken );
96+ }
97+
98+ unset( $ story );
99+ ?>
100+ <!DOCTYPE html>
101+ <html>
102+ <head>
103+ <title>
104+ Instgram Graph API | IG User | Stories
105+ </title>
106+ </head>
107+ <body>
108+ <h1>
109+ Instgram Graph API | IG User | Stories
110+ </h1>
111+ <h2>
112+ <!-- display syntax for reference -->
113+ <?php echo $ endpointSyntax ; ?>
114+ </h2>
115+ <h3>
116+ User Stories Response
117+ </h3>
118+ <div>
119+ <!-- dump out the entire response -->
120+ <pre><?php print_r ( $ stories ); ?> </pre>
121+ </div>
122+ <hr />
123+ <?php foreach ( $ stories as $ story ) : // loop over each story element ?>
124+ <?php if ( 'VIDEO ' == $ story ['media_info ' ]['media_type ' ] ) : // story media is a video ?>
125+ <div>
126+ <video controls poster="<?php echo $ story ['media_info ' ]['thumbnail_url ' ]; ?> " style="max-width:300px">
127+ <source src="<?php echo $ story ['media_info ' ]['media_url ' ]; ?> " />
128+ </video>
129+ </div>
130+ <?php elseif ( 'IMAGE ' == $ story ['media_info ' ]['media_type ' ] ) : // story media is an image ?>
131+ <div>
132+ <img src="<?php echo $ story ['media_info ' ]['media_url ' ]; ?> " style="max-width:300px" />
133+ </div>
134+ <?php endif ; ?>
135+ <div>
136+ <b>
137+ <?php echo $ story ['media_info ' ]['username ' ]; ?>
138+ </b>
139+ </div>
140+ <a href="<?php echo $ story ['media_info ' ]['permalink ' ]; ?> " target="_blank">
141+ View on Instagram
142+ </a>
143+ <?php endforeach ; ?>
144+ </body>
145+ </html>
0 commit comments