Skip to content

Commit e82f8db

Browse files
committed
instagram graph api comment and reply
1 parent 0359cba commit e82f8db

3 files changed

Lines changed: 177 additions & 3 deletions

File tree

instagram_graph_api/business_discovery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
include 'defines.php';
33

44
// get instagram user metadata endpoint
5-
$endpointFormat = ENDPOINT_BASE . '{ig-user-id}?fields=business_discovery.username({ig-username}){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}&access_token={access-token}';
5+
$endpointFormat = ENDPOINT_BASE . '{ig-user-id}?fields=business_discovery.username({ig-username}){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count,media{caption,like_count,comments_count,media_url,permalink,media_type}}&access_token={access-token}';
66
$endpoint = ENDPOINT_BASE . $instagramAccountId;
77

88
// username
99
$username = 'IG-USER-NAME';
1010

1111
// endpoint params
1212
$igParams = array(
13-
'fields' => 'business_discovery.username(' . $username . '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count}',
13+
'fields' => 'business_discovery.username(' . $username . '){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count,media{caption,like_count,comments_count,media_url,permalink,media_type}}',
1414
'access_token' => $accessToken
1515
);
1616

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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>

instagram_graph_api/obtaining_access_token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
echo '<h1>Long Lived Access Token</h1>';
4646
print_r( $accessToken );
4747
} else { // display login url
48-
$permissions = ['public_profile', 'instagram_basic', 'pages_show_list'];
48+
$permissions = ['public_profile', 'instagram_basic', 'pages_show_list', 'instagram_manage_insights', 'instagram_manage_comments', 'manage_pages'];
4949
$loginUrl = $helper->getLoginUrl( FACEBOOK_REDIRECT_URI, $permissions );
5050

5151
echo '<a href="' . $loginUrl . '">

0 commit comments

Comments
 (0)