1+ <?php
2+ include 'defines.php ' ;
3+
4+ function makeApiCall ( $ endpoint , $ type , $ params ) {
5+ $ ch = curl_init ();
6+
7+ if ( 'POST ' == $ type ) {
8+ curl_setopt ( $ ch , CURLOPT_URL , $ endpoint );
9+ curl_setopt ( $ ch , CURLOPT_POSTFIELDS , http_build_query ( $ params ) );
10+ curl_setopt ( $ ch , CURLOPT_POST , 1 );
11+ } elseif ( 'GET ' == $ type ) {
12+ curl_setopt ( $ ch , CURLOPT_URL , $ endpoint . '? ' . http_build_query ( $ params ) );
13+ }
14+
15+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYHOST , false );
16+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , false );
17+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
18+
19+ $ response = curl_exec ( $ ch );
20+ curl_close ( $ ch );
21+
22+ return json_decode ( $ response , true );
23+ }
24+
25+ $ hashtag = 'coding ' ;
26+ $ hashtagId = '17841563020115819 ' ;
27+
28+ // ENDPOINT_BASE = https://graph.facebook.com/v5.0/
29+ $ hashtagSearchEndpoingFormat = ENDPOINT_BASE . 'ig_hashtag_search?user_id={user-id}&q={hashtag-name}&fields=id,name ' ;
30+ $ hashtagDataEndpointFormat = ENDPOINT_BASE . '{hashtag-id}?fields=id,name ' ;
31+ $ hashtagTopMediaEndpointFormat = ENDPOINT_BASE . '{ig-hashtag-id}/top_media?user_id={user-id}&fields=id,caption,children,comments_count,like_count,media_type,media_url,permalink ' ;
32+ $ hashtagRecentEndpointFormat = ENDPOINT_BASE . '{ig-hashtag-id}/recent_media?user_id={user-id}&fields=id,caption,children,comments_count,like_count,media_type,media_url,permalink ' ;
33+
34+ // get hashtag by name
35+ $ hashtagSearchEndpoint = ENDPOINT_BASE . 'ig_hashtag_search ' ;
36+ $ hashtagSearchParams = array (
37+ 'user_id ' => $ instagramAccountId ,
38+ 'fields ' => 'id,name ' ,
39+ 'q ' => $ hashtag ,
40+ 'access_token ' => $ accessToken
41+ );
42+ $ hashtagSearch = makeApiCall ( $ hashtagSearchEndpoint , 'GET ' , $ hashtagSearchParams );
43+
44+ // get hashtag by id
45+ $ hashtagDataEndpoint = ENDPOINT_BASE . $ hashtagId ;
46+ $ hashtagDataParams = array (
47+ 'fields ' => 'id,name ' ,
48+ 'access_token ' => $ accessToken
49+ );
50+ $ hashtagData = makeApiCall ( $ hashtagDataEndpoint , 'GET ' , $ hashtagDataParams );
51+
52+ // top media for hashtag
53+ $ hashtagTopMediaEndpoint = ENDPOINT_BASE . $ hashtagId . '/top_media ' ;
54+ $ hashtagTopMediaParams = array (
55+ 'user_id ' => $ instagramAccountId ,
56+ 'fields ' => 'id,caption,children,comments_count,like_count,media_type,media_url,permalink ' ,
57+ 'access_token ' => $ accessToken
58+ );
59+ $ hashtagTopMedia = makeApiCall ( $ hashtagTopMediaEndpoint , 'GET ' , $ hashtagTopMediaParams );
60+ $ topPost = $ hashtagTopMedia ['data ' ][0 ];
61+
62+ // recent media
63+ $ hashtagRecentEndpoint = ENDPOINT_BASE . $ hashtagId . '/recent_media ' ;
64+ $ hashtagRecentParams = array (
65+ 'user_id ' => $ instagramAccountId ,
66+ 'fields ' => 'id,caption,children,comments_count,like_count,media_type,media_url,permalink ' ,
67+ 'access_token ' => $ accessToken
68+ );
69+ $ hashtagRecent = makeApiCall ( $ hashtagRecentEndpoint , 'GET ' , $ hashtagRecentParams );
70+ $ recentPost = $ hashtagRecent ['data ' ][0 ];
71+ ?>
72+ <!DOCTYPE html>
73+ <html>
74+ <head>
75+ <title>
76+ Hashtag Search with the Instagram Graph API
77+ </title>
78+ <meta charset="utf-8" />
79+ </head>
80+ <body>
81+ <h1>
82+ Hashtag Search with the Instagram Graph API
83+ </h1>
84+ <hr />
85+ <h2>
86+ Hashtag
87+ <a target="_blank" href="https://www.instagram.com/explore/tags/<?php echo $ hashtag ; ?> ">
88+ #<?php echo $ hashtag ; ?> (id <?php echo $ hashtagData ['id ' ]; ?> )
89+ </a>
90+ <h2>
91+ <hr />
92+ <h3>
93+ Top Post for #<?php echo $ hashtagData ['name ' ]; ?>
94+ </h3>
95+ <div>
96+ <div>
97+ <?php if ( 'IMAGE ' == $ topPost ['media_type ' ] || 'CAROUSEL_ALBUM ' == $ topPost ['media_type ' ] ) : ?>
98+ <img style="height:320px" src="<?php echo $ topPost ['media_url ' ]; ?> " />
99+ <?php else : ?>
100+ <video height="240" width="320" controls>
101+ <source src="<?php echo $ topPost ['media_url ' ]; ?> " />
102+ </video>
103+ <?php endif ; ?>
104+ </div>
105+ <div>
106+ <b>Caption: <?php echo nl2br ( $ topPost ['caption ' ] ); ?> </b>
107+ </div>
108+ <div>
109+ Post ID: <?php echo nl2br ( $ topPost ['id ' ] ); ?>
110+ </div>
111+ <div>
112+ Media Type: <?php echo nl2br ( $ topPost ['media_type ' ] ); ?>
113+ </div>
114+ <div>
115+ Media URL: <?php echo nl2br ( $ topPost ['media_url ' ] ); ?>
116+ </div>
117+ <div>
118+ Link: <a target="_blank" href="<?php echo $ topPost ['permalink ' ]; ?> "><?php echo $ topPost ['permalink ' ]; ?> </a>
119+ </div>
120+ </div>
121+ <hr />
122+ <h3>
123+ Recent for #<?php echo $ hashtagData ['name ' ]; ?>
124+ </h3>
125+ <div>
126+ <div>
127+ <?php if ( 'IMAGE ' == $ recentPost ['media_type ' ] || 'CAROUSEL_ALBUM ' == $ recentPost ['media_type ' ] ) : ?>
128+ <img style="height:320px" src="<?php echo $ recentPost ['media_url ' ]; ?> " />
129+ <?php else : ?>
130+ <video height="240" width="320" controls>
131+ <source src="<?php echo $ recentPost ['media_url ' ]; ?> " />
132+ </video>
133+ <?php endif ; ?>
134+ </div>
135+ <div>
136+ <b>Caption: <?php echo nl2br ( $ recentPost ['caption ' ] ); ?> </b>
137+ </div>
138+ <div>
139+ Post ID: <?php echo nl2br ( $ recentPost ['id ' ] ); ?>
140+ </div>
141+ <div>
142+ Media Type: <?php echo nl2br ( $ recentPost ['media_type ' ] ); ?>
143+ </div>
144+ <div>
145+ Media URL: <?php echo nl2br ( $ recentPost ['media_url ' ] ); ?>
146+ </div>
147+ <div>
148+ Link: <a target="_blank" href="<?php echo $ recentPost ['permalink ' ]; ?> "><?php echo $ recentPost ['permalink ' ]; ?> </a>
149+ </div>
150+ </div>
151+ <hr />
152+ <h3>
153+ Hashtag Search Endpoint: <?php echo $ hashtagSearchEndpoint ; ?>
154+ </h3>
155+ <textarea style="width:100%;height:200px"><?php print_r ( $ hashtagSearch ); ?> </textarea>
156+ <hr />
157+ <h3>
158+ Hashtag Data Endpoint: <?php echo $ hashtagDataEndpoint ; ?>
159+ </h3>
160+ <textarea style="width:100%;height:100px"><?php print_r ( $ hashtagData ); ?> </textarea>
161+ <hr />
162+ <h3>
163+ Hashtag Top Media Endpoint: <?php echo $ hashtagTopMediaEndpoint ; ?>
164+ </h3>
165+ <textarea style="width:100%;height:300px"><?php print_r ( $ hashtagTopMedia ); ?> </textarea>
166+ <hr />
167+ <h3>
168+ Hashtag Recent Media Endpoint: <?php echo $ hashtagRecentEndpoint ; ?>
169+ </h3>
170+ <textarea style="width:100%;height:300px"><?php print_r ( $ hashtagRecent ); ?> </textarea>
171+ </body>
172+ </html>
0 commit comments