I am working on a website and am looking to retrieve the 6 most recent photos from an instagram account and make the caption the hover text. My code below works, but what I notice is that after I refresh the page once or twice, I start getting this error:
Warning: file_get_contents(http://instagram.com/green_tree_relief): failed to open stream: HTTP request failed! HTTP/1.1 503 No server is available for the request on line 14
I assume this is instagram blocking me for my behavior?
I am looking for a way around this, if possible. I tried to spoof a user agent and that didn't work. If I am not misunderstanding their API deprecation, they won't allow you to use it to get even public content. I can see this as an ethically gray area I suppose, so abandoning this functionality altogether is an option, but I would like to get this to work.
Anyways, on my main page there is an ajax call to this PHP script which then inserts the generated HTML upon success:
function scrape_insta($username) {
$insta_source = file_get_contents('http://instagram.com/'.$username);
$shards = explode('window._sharedData = ', $insta_source);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
return $insta_array;
}
$my_account = 'green_tree_relief';
$photostreamHTML = '';
$results_array = scrape_insta($my_account);
for($i = 0; $i < 6; $i++) {
if(isset($results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_to_caption']['edges'][0]['node']['text'])) {
$caption = $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_to_caption']['edges'][0]['node']['text'];
}
if(isset($results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['display_url'])) {
$photostreamHTML .= '<div style="height: 84px;">
<a href="https://www.instagram.com/' . $my_account . '" target="_blank">
<img src="'
. $results_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['display_url'] . '"
class ="img-responsive" title = "' . $caption . '">
</a>
</div>';
}
}
As I said, it works the first couple of times I load the page when I haven't been working on the site for a few hours but then it fails after that.
Any suggestions would be appreciated.