1+ <?php
2+ include 'defines.php ' ;
3+
4+ $ mediaObject = array ( // media post we are working with
5+ 'id ' => '17908649842399636 ' ,
6+ 'caption ' => "Can you spot me in the video?! 😆🤣 Coding tonight against the Instagram Graph API and getting a users metadata!
7+ .
8+ The metadata we can get from the Instagram Graph API for a user includes, profile image url, account I'd, username, website, name, biography, follow count, follower count, media count.
9+ .
10+ #coding #instagram #coder #tech #php #html #fullstackdeveloper #webdevelopment #webstagram #computers #frontenddeveloper #instagramgraphapi #instagramapi #api #backend #website #softwareengineer #code #programming #facebook " ,
11+ 'media_url ' => 'https://scontent.xx.fbcdn.net/v/t50.31694-16/82877274_168580704395389_6442072919343833857_n.mp4?_nc_cat=102&_nc_ohc=pfuRI1wD3twAX8AvCD6&_nc_ht=scontent.xx&oh=225a7ef08d9a72cf7f5b40580cb45923&oe=5ED484E7 ' ,
12+ 'permalink ' => 'https://www.instagram.com/p/B7pYqdPAezS/ ' ,
13+ 'media_type ' => "VIDEO "
14+ );
15+
16+ function makeApiCall ( $ endpoint , $ type , $ params ) {
17+ $ ch = curl_init ();
18+
19+ if ( 'POST ' == $ type ) {
20+ curl_setopt ( $ ch , CURLOPT_URL , $ endpoint );
21+ curl_setopt ( $ ch , CURLOPT_POSTFIELDS , http_build_query ( $ params ) );
22+ curl_setopt ( $ ch , CURLOPT_POST , 1 );
23+ } elseif ( 'GET ' == $ type ) {
24+ curl_setopt ( $ ch , CURLOPT_URL , $ endpoint . '? ' . http_build_query ( $ params ) );
25+ }
26+
27+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , false );
28+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false );
29+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
30+
31+ $ response = curl_exec ( $ ch );
32+ curl_close ( $ ch );
33+
34+ return json_decode ( $ response , true );
35+ }
36+
37+ // endpoint formats
38+ $ commentsEndpointFormat = ENDPOINT_BASE . '{ig-media-id}/comments?fields=like_count,replies,username,text ' ;
39+ $ repliesEndpointFormat = ENDPOINT_BASE . '{ig-comment-id}/replies?fields=username,text,like_count ' ;
40+ $ postCommentEndpointFormat = ENDPOINT_BASE . '{ig-media-id}/comments?message={message} ' ;
41+ $ postReplyEndpointFormat = ENDPOINT_BASE . '{ig-comment-id}/replies?message={message} ' ;
42+
43+ // post comment to IG
44+ // $postCommentEndpoint = ENDPOINT_BASE . $mediaObject['id'] . '/comments';
45+ // $postCommentIgParams = array(
46+ // 'message' => 'Commenting from IG Graph API!! :)',
47+ // 'access_token' => $accessToken
48+ // );
49+ // $postCommentResponseArray = makeApiCall( $postCommentEndpoint, 'POST', $postCommentIgParams );
50+ // echo '<pre>';
51+ // print_r($postCommentResponseArray);
52+ // die();
53+
54+ // post reply to comment
55+ // $commentId = '17982899548288082';
56+ // $postReplyEndpoint = ENDPOINT_BASE . $commentId . '/replies';
57+ // $postReplyIgParams = array(
58+ // 'message' => 'Reply coming from IG Graph API!! :)',
59+ // 'access_token' => $accessToken
60+ // );
61+ // $postReplyResponseArray = makeApiCall( $postReplyEndpoint, 'POST', $postReplyIgParams );
62+ // echo '<pre>';
63+ // print_r($postReplyResponseArray);
64+ // die();
65+
66+
67+ // get comments from IG
68+ $ commentsEndpoint = ENDPOINT_BASE . $ mediaObject ['id ' ] . '/comments ' ;
69+ $ igParams = array (
70+ 'fields ' => 'like_count,replies,username,text ' ,
71+ 'access_token ' => $ accessToken
72+ );
73+ $ responseArray = makeApiCall ( $ commentsEndpoint , 'GET ' , $ igParams );
74+ ?>
75+ <!DOCTYPE html>
76+ <html>
77+ <head>
78+ <title>Getting and Replying to Comments on Instagram with the Instagram Graph API</title>
79+ <meta charset="utf-8" />
80+ </head>
81+ <body>
82+ <h1>Getting and Replying to Comments on Instagram with the Instagram Graph API</h1>
83+ <hr />
84+ <br />
85+ <div style="width:100%;display:inline-block">
86+ <div style="float:left">
87+ <video height="390" width="310" controls="">
88+ <source src="<?php echo $ mediaObject ['media_url ' ]; ?> ">
89+ </video>
90+ </div>
91+ <div style="float:left;margin-left:20px;">
92+ <a target="_blank" href="<?php echo $ mediaObject ['permalink ' ]; ?> ">
93+ <h3>
94+ <?php echo $ mediaObject ['caption ' ]; ?>
95+ </h3>
96+ </a>
97+ </div>
98+ </div>
99+ <br />
100+ <h4>
101+ Comments
102+ </h4>
103+ <ul style="list-style: none">
104+ <?php foreach ( $ responseArray ['data ' ] as $ comment ) : ?>
105+ <?php
106+ // get comment replies from instagram
107+ $ repliesEndpoint = ENDPOINT_BASE . $ comment ['id ' ] . '/replies ' ;
108+ $ repliesIgParams = array (
109+ 'fields ' => 'username,text,like_count ' ,
110+ 'access_token ' => $ accessToken
111+ );
112+ $ repliesResponseArray = makeApiCall ( $ repliesEndpoint , 'GET ' , $ repliesIgParams );
113+ ?>
114+ <li style="margin-top:20px;margin-bottom:20px">
115+ <div>
116+ <a href="https://instagram.com/<?php echo $ comment ['username ' ]; ?> " target="_blank">
117+ <b><?php echo $ comment ['username ' ]; ?> </b>
118+ </a>
119+ </div>
120+ <div>
121+ <?php echo $ comment ['text ' ]; ?>
122+ </div>
123+ <div style="font-size:10px">
124+ Likes <?php echo $ comment ['like_count ' ]; ?>
125+ </div>
126+ <div>
127+ <h5>
128+ Replies (<?php echo count ( $ repliesResponseArray ['data ' ] ); ?> )
129+ </h5>
130+ </div>
131+ <div style="margin-left:20px">
132+ <ul style="list-style: none">
133+ <?php foreach ( $ repliesResponseArray ['data ' ] as $ reply ) : ?>
134+ <div>
135+ <a href="https://instagram.com/<?php echo $ reply ['username ' ]; ?> " target="_blank">
136+ <b><?php echo $ reply ['username ' ]; ?> </b>
137+ </a>
138+ </div>
139+ <div>
140+ <?php echo $ reply ['text ' ]; ?>
141+ </div>
142+ <div style="font-size:10px">
143+ Likes <?php echo $ reply ['like_count ' ]; ?>
144+ </div>
145+ <?php endforeach ; ?>
146+ </ul>
147+ </div>
148+ </li>
149+ <?php endforeach ; ?>
150+ </ul>
151+ <br />
152+ <hr />
153+ <h3>
154+ Get Comments Endpoint: <?php echo $ commentsEndpointFormat ; ?>
155+ </h3>
156+ <hr />
157+ <h3>
158+ Get Replies Endpoint: <?php echo $ repliesEndpointFormat ; ?>
159+ </h3>
160+ <hr />
161+ <h3>
162+ Post Comments Endpoint: <?php echo $ postCommentEndpointFormat ; ?>
163+ </h3>
164+ <hr />
165+ <h3>
166+ Post Replies Endpoint: <?php echo $ postReplyEndpointFormat ; ?>
167+ </h3>
168+ <hr />
169+ <h3>
170+ Get Comments Raw Response
171+ </h3>
172+ <textarea style="width:100%;height:300px"><?php print_r ( $ responseArray ); ?> </textarea>
173+ </body>
174+ </html>
0 commit comments