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-media-id}?fields={fields}&access_token={access-token} ' ;
7+
8+ // actual endpoint with a medi aid
9+ $ endpoint = 'https://graph.facebook.com/v11.0/18133500736207075 ' ;
10+
11+ $ params = array ( // parameters for the endpoint
12+ 'fields ' => 'caption,comments_count,id,ig_id,is_comment_enabled,like_count,media_product_type,media_type,media_url,owner,permalink,shortcode,thumbnail_url,timestamp,username,video_title ' ,
13+ 'access_token ' => $ accessToken
14+ );
15+
16+ // make the api call and get a response
17+ $ response = makeApiCall ( $ endpoint , 'GET ' , $ params );
18+
19+ /**
20+ * Make a a curl call to an endpoint with params
21+ *
22+ * @param string $endpoint we are hitting
23+ * @param string $type of request
24+ * @param array $params to send along with the request
25+ *
26+ * @return array with the api response
27+ */
28+ function makeApiCall ( $ endpoint , $ type , $ params ) {
29+ // initialize curl
30+ $ ch = curl_init ();
31+
32+ // create endpoint with params
33+ $ apiEndpoint = $ endpoint . '? ' . http_build_query ( $ params );
34+
35+ // set other curl options
36+ curl_setopt ( $ ch , CURLOPT_URL , $ apiEndpoint );
37+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
38+
39+ // get response
40+ $ response = curl_exec ( $ ch );
41+
42+ // close curl
43+ curl_close ( $ ch );
44+
45+ return array ( // return data
46+ 'type ' => $ type ,
47+ 'endpoint ' => $ endpoint ,
48+ 'params ' => $ params ,
49+ 'api_endpoint ' => $ apiEndpoint ,
50+ 'data ' => json_decode ( $ response , true )
51+ );
52+ }
53+ ?>
54+ <!DOCTYPE html>
55+ <html>
56+ <head>
57+ <title>
58+ Instgram Graph API | IG Media
59+ </title>
60+ </head>
61+ <body>
62+ <h1>
63+ <!-- display syntax for reference -->
64+ <?php echo $ endpointSyntax ; ?>
65+ </h1>
66+ <h3>
67+ Response
68+ </h3>
69+ <div>
70+ <!-- dump out the entire response -->
71+ <pre><?php print_r ( $ response ); ?> </pre>
72+ </div>
73+ <hr />
74+ <div>
75+ <video controls poster="<?php echo $ response ['data ' ]['thumbnail_url ' ]; ?> ">
76+ <source src="<?php echo $ response ['data ' ]['media_url ' ]; ?> ">
77+ </video>
78+ </div>
79+ <div>
80+ <b>
81+ <!-- display likes count -->
82+ <?php echo $ response ['data ' ]['like_count ' ]; ?> Likes
83+ </b>
84+ </div>
85+ <div>
86+ <b>
87+ <!-- display username from response -->
88+ <?php echo $ response ['data ' ]['username ' ]; ?>
89+ </b>
90+ <!-- display caption from response -->
91+ <?php echo nl2br ( $ response ['data ' ]['caption ' ] ); ?>
92+ </div>
93+ <div>
94+ <b>
95+ <!-- display comments count from response -->
96+ <?php echo $ response ['data ' ]['comments_count ' ]; ?> Comments
97+ </b>
98+ </div>
99+ <!-- link to the actual post on instagram -->
100+ <a href="<?php echo $ response ['data ' ]['permalink ' ]; ?> " target="_blank">
101+ View on Instagram
102+ </a>
103+ </body>
104+ </html>
0 commit comments