Forked from joshrathke/instagram_advanced_query.php
Last active
August 29, 2015 14:21
-
-
Save tonycaputome/97929906a45ecb6af61f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* By default instagram doesn't allow us to query for more than | |
* attribute, such as media posted by a specific author tagged with | |
* a specific tag. In order to accomplish this we will acquire all of | |
* the media tagged with the tag we are looking for and then filter it | |
* down based on the user. | |
*/ | |
// The tag you want to query | |
$tag = 'THE_TAG_YOU_WANT_TO_QUERY'; | |
/** | |
* Query variables for the CURL URL | |
* For simple queries the client ID alone will suffice. | |
* We also need a very high number for the requested amount | |
* of posts so that the App continues to give us a paginated | |
* URL that allows us to iterate through all of the media. | |
*/ | |
$query = array( | |
'client_id' => 'CLIENT_ID', | |
'count' => 999999 | |
); | |
// Set the url for the first iteration of the CURL | |
$url = "https://api.instagram.com/v1/tags/{$tag}/media/recent?".http_build_query($query); | |
/** | |
* Instagram will only give us 33 posts at a time, so at the end of the CURL | |
* we will reset the $url using the next_url atribute in the pagination section | |
* of the returned object. | |
*/ | |
while ($url) { | |
try { | |
$curl_connection = curl_init($url); | |
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); | |
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); | |
//Data are stored in $data | |
$data = json_decode(curl_exec($curl_connection), true); | |
curl_close($curl_connection); | |
/** | |
* Loop through all of the posts retrieved in the latest CURL and assign | |
* them to our $instagram_posts variable if they were authored by the user | |
* we are looking for. | |
*/ | |
if ( !empty( $data['data'] ) ) { | |
foreach ($data['data'] as $post_tag_match) { | |
if ( $post_tag_match['user']['username'] == 'USERNAME_TO_FILTER' ) { | |
$instagram_posts[] = $post_tag_match; | |
} | |
} | |
} | |
} catch(Exception $e) { | |
return $e->getMessage(); | |
} | |
// Reset the $url variable using the next_url attribute. | |
$url = $data['pagination']['next_url']; | |
} | |
// Display all of the posts | |
foreach ( $instagram_posts as $post ) { | |
echo '<img src="' . $post['images']['standard_resolution']['url'] . '" />'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment