0

I have a script built that shows a "ticker" of a clients Instagram followers.

A few weeks (or maybe months) back, it stopped working, and has just shown "0".

I have tried both file_get_contents as well a cURL, and neither seem to work.

Or perhaps more accurately... they work for approximately 5 seconds, and then stop again.

What's strange is, if I go to the graph URL (https://www.instagram.com/account_name_here/?__a=1), I can see the JSON that it spits out, no matter how many times I reload the page.

But, running the following code:

$ch2 = curl_init();
$url = "https://www.instagram.com/account_name_here/?__a=1";
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_URL,$url);
$result=curl_exec($ch2);
curl_close($ch2);

$data = json_decode($result, true);
$followerCount = $data['graphql']['user']['edge_followed_by']['count'];
echo "Followers: ".$followerCount;

Spits out nothing.

Doing a var_dump() on various other pieces of this, including $result and $data reinforce the idea that nothing indeed is getting pulled in.

If I do var_dump($result) for example, it spits out:

string(0)""

It is particularly infuriating that the first time I switched to using cURL over file_get_contents, it worked. And then as soon as I uploaded it to the website, it stopped working again.

Any idea what I am missing here? Why when I go to the actual instagram URL I see the data fine, but as soon as I try to pull it via PHP, it doesn't work?

TIA!

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
John Hubler
  • 877
  • 1
  • 11
  • 27
  • Works perfectly on my machine. When your $result is empty make sure you have installed php-curl and no proxy or firewall blocks the outgoing request. Look with [curl_error($ch2)](https://www.php.net/manual/en/function.curl-error) what's wrong. – Markus Zeller Feb 17 '21 at 15:41

1 Answers1

0

Consider using the following code to 'debug' the issue:

$info = curl_getinfo($ch2);
echo "Followers: " . $followerCount . PHP_EOL;
echo "HTTP: " . $info['http_code'] . PHP_EOL;
echo "URL: " . $info['url'] . PHP_EOL;

Logging this, while having the same issue as you described (first 3 requests works fine, then NULL is shown shows this;

NULL
Followers: ?
HTTP: 200
URL: https://www.instagram.com/accounts/login/?next=/account-name/%3F__a%3D1

The issue seems that Instagram is redirecting the request to a login page.

Please take a look at this answer regarding API login requests.

0stone0
  • 34,288
  • 4
  • 39
  • 64