1

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.

Soulfire
  • 4,218
  • 23
  • 33
  • Does your PHP config generally allow accessing URLs via `file_get_contents` and also with HTTPS? See https://stackoverflow.com/a/7499529/2048874 – Loilo Jun 12 '18 at 19:55
  • For the record, I can do `file_get_contents("http://instagram.com/green_tree_relief")` without any problems, that's why I'd guess that your issue is config-related. – Loilo Jun 12 '18 at 19:56
  • If it didn't do you think I would get the 503 error? I ask because if my understanding correct, 503 is basically "I see you, but I can't help you right at this moment". It does work fine the first couple times I load my page. I will look into your suggestion, thank you. – Soulfire Jun 13 '18 at 12:58
  • No, I actually think you'd get a PHP error. But it was worth a try I guess. ;) However, I don't think you'd get a 503 either if the service didn't want you on their side. Sure, they could send whatever they want, but those cases are usually answered with a 4xx status code... – Loilo Jun 13 '18 at 13:01
  • It does appear I am set for https, too, based on the suggestions in that question you provided. Thanks for looking in to it I appreciate the help. Could it be because I am using Xampp and localhost for right now? – Soulfire Jun 13 '18 at 13:02
  • I don't think so, my successful test run was from an XAMPP installation as well (from the command line to be precise, via `php -r "echo file_get_contents('http://instagram.com/green_tree_relief');"`). – Loilo Jun 13 '18 at 13:08

0 Answers0