LeonidMew
LeonidMew

Reputation: 502

Get JSON of instagram post

I'm trying to get json of instagram post. The url to fetch data:

$url = "https://www.instagram.com/p/" . $_GET['media'] . "/?__a=1";

When I'm get this url in browser - all fine, json returned. Btw browser not logged in to instagram. But when I use curl or file_get_contents($url); it return http code 302 with header 'location' to login page.

Code sample:

$url = "https://www.instagram.com/p/" . $_GET['media'] . "/?__a=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
$data = curl_exec($ch);
curl_close($ch);

var_dump($data);

Maybe curl options should be modified to look like browser.

Update

I have try same headers as in browser, include cookie, but no luck, seems this impossible. I have already written page using this hack, what stop working recently. Using API means rewrite everything as data in response is different.

Upvotes: 2

Views: 6033

Answers (1)

Torxed
Torxed

Reputation: 23480

The reason for this is quite simple, you're trying to access a web-resource that's protected by authentication (this should be obvious) or protected by detecting non-standard behavior. The reason for your curl request failing is because it's missing a Cookie header or some form of other header needed to identify you as a human. Usually it's the cookie identifying you and your authenticated session as trusted with the server. At some point, you've most likely logged in with your browser and that's why the request works in your browser - but not the curl/php logic or you're missing headers such as User-Agent that masks the use of curl.

enter image description here Here's an example of a cookie string identifying me as myself. Without it, I wouldn't be able to do these requests in my browser. There for, as long as the server sends Set-Cookie: ... the browser honors it and saves it, keeps track of it and sends it with every request.

Either you borrow a cookie from your browser session and implement it temporarily into your curl requests, or you implement the login logic before sending curl requests. But you should do the right thing and start using the Instagram API as pointed out by Magnus Eriksson in the comments.

The later is the recommended, and there are some libraries altho they are old. But perhaps they will give you an idea of how to go about it.

Instagram-PHP-API library as an example.

use MetzWeb\Instagram\Instagram;

$instagram = new Instagram(array(
    'apiKey'      => 'YOUR_APP_KEY',
    'apiSecret'   => 'YOUR_APP_SECRET',
    'apiCallback' => 'YOUR_APP_CALLBACK'
));

echo "<a href='{$instagram->getLoginUrl()}'>Login with Instagram</a>";

And if you're left wondering, "what the hell is an API", here's (Tom Scott - This Video Has X Views) a video to a good explanation and why it's not a good idea to pretend to be a human - but instead use API's.

Upvotes: 5

Related Questions