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+ $ mediaInsightsEndpoingFormat = ENDPOINT_BASE . '{ig-media-id}/insights?metric=engagement,impressions,reach,saved,video_views&access_token={access-token} ' ;
38+ $ userInsightsEndpoingFormat = ENDPOINT_BASE . '{ig-user-id}/insights?metric=follower_count,impressions,profile_views,reach&period=day&access_token={access-token} ' ;
39+
40+ // get media insights
41+ $ mediaInsightsEndpoint = ENDPOINT_BASE . $ mediaObject ['id ' ] . '/insights ' ;
42+ $ mediaInsightParams = array (
43+ 'metric ' => 'engagement,impressions,reach,saved,video_views ' ,
44+ 'access_token ' => $ accessToken
45+ );
46+ $ mediaInsights = makeApiCall ( $ mediaInsightsEndpoint , 'GET ' , $ mediaInsightParams );
47+
48+ // get user insights
49+ $ userInsightsEndpoint = ENDPOINT_BASE . $ instagramAccountId . '/insights ' ;
50+ $ userInsightParams = array (
51+ 'metric ' => 'follower_count,impressions,profile_views,reach ' ,
52+ 'period ' => 'day ' ,
53+ 'access_token ' => $ accessToken
54+ );
55+ $ userInsights = makeApiCall ( $ userInsightsEndpoint , 'GET ' , $ userInsightParams );
56+
57+ // get instagram user metadata endpoint
58+ $ userInfoEndpointFormat = 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} ' ;
59+ $ userInfoEndpoint = ENDPOINT_BASE . $ instagramAccountId ;
60+ $ username = 'justinstolpe ' ;
61+ // endpoint params
62+ $ userInfoParams = array (
63+ '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}} ' ,
64+ 'access_token ' => $ accessToken
65+ );
66+ $ userInfo = makeApiCall ( $ userInfoEndpoint , 'GET ' , $ userInfoParams );
67+ ?>
68+ <!DOCTYPE html>
69+ <html>
70+ <head>
71+ <title>Getting Insights on Instagram Posts and User Accounts with the Instagram Graph API</title>
72+ <meta charset="utf-8" />
73+ </head>
74+ <body>
75+ <h1>Getting Insights on Instagram Posts and User Accounts with the Instagram Graph API</h1>
76+ <hr />
77+ <br />
78+ <h2>User Info</h2>
79+ <div style="display:inline-block;width:100%">
80+ <div style="float:left">
81+ <img style="width:50px;border-radius:50%;" src="<?php echo $ userInfo ['business_discovery ' ]['profile_picture_url ' ]; ?> " />
82+ </div>
83+ <div style="float:left;margin-left:20px">
84+ <a target="_blank" href="https://www.instagram.com/<?php echo $ userInfo ['business_discovery ' ]['username ' ]; ?> ">
85+ <h3><?php echo $ userInfo ['business_discovery ' ]['username ' ]; ?> </h3>
86+ </a>
87+ <div style="display:inline-block">
88+ <b><?php echo $ userInfo ['business_discovery ' ]['media_count ' ]; ?> </b> posts
89+ </div>
90+ <div style="display:inline-block;margin-left:20px">
91+ <b><?php echo $ userInfo ['business_discovery ' ]['followers_count ' ]; ?> </b> followers
92+ </div>
93+ <div style="display:inline-block;margin-left:20px">
94+ <b><?php echo $ userInfo ['business_discovery ' ]['follows_count ' ]; ?> </b> following
95+ </div>
96+ </div>
97+ </div>
98+ <h2>User Insights</h2>
99+ <ul>
100+ <?php foreach ( $ userInsights ['data ' ] as $ insight ) : ?>
101+ <li>
102+ <div>
103+ <b><?php echo $ insight ['title ' ]; ?> </b>
104+ </div>
105+ <div>
106+ <?php foreach ( $ insight ['values ' ] as $ value ) : ?>
107+ <div>Value: <?php echo $ value ['value ' ]; ?> <?php echo $ value ['end_time ' ]; ?>
108+ <?php endforeach ; ?>
109+ </div>
110+ </li>
111+ <?php endforeach ; ?>
112+ </ul>
113+ <br />
114+ <hr />
115+ <br />
116+ <h2>Media Object<h2>
117+ <div style="width:100%;display:inline-block">
118+ <div style="float:left">
119+ <video height="390" width="310" controls="">
120+ <source src="<?php echo $ mediaObject ['media_url ' ]; ?> ">
121+ </video>
122+ </div>
123+ <div style="float:left;margin-left:20px;">
124+ <a target="_blank" href="<?php echo $ mediaObject ['permalink ' ]; ?> ">
125+ <h3>
126+ <?php echo $ mediaObject ['caption ' ]; ?>
127+ </h3>
128+ </a>
129+ </div>
130+ </div>
131+ <h3>Media Object Insights</h3>
132+ <ul>
133+ <?php foreach ( $ mediaInsights ['data ' ] as $ insight ) : ?>
134+ <li>
135+ <div>
136+ <b><?php echo $ insight ['title ' ]; ?> </b>
137+ </div>
138+ <div>
139+ <?php foreach ( $ insight ['values ' ] as $ value ) : ?>
140+ <div>Value: <?php echo $ value ['value ' ]; ?> <?php echo $ value ['end_time ' ]; ?>
141+ <?php endforeach ; ?>
142+ </div>
143+ </li>
144+ <?php endforeach ; ?>
145+ </ul>
146+ <h3>
147+ Get Media Insights Endpoint: <?php echo $ mediaInsightsEndpoingFormat ; ?>
148+ </h3>
149+ <h4>Response<h4>
150+ <textarea style="width:100%;height:300px"><?php print_r ( $ mediaInsights ); ?> </textarea>
151+ <h3>
152+ Get User Insights Endpoint: <?php echo $ userInsightsEndpoingFormat ; ?>
153+ </h3>
154+ <h4>Response<h4>
155+ <textarea style="width:100%;height:300px"><?php print_r ( $ userInsights ); ?> </textarea>
156+ </body>
157+ </html>
0 commit comments