sisaln
sisaln

Reputation: 220

Get instagram public JSON feed with cURL PHP

I want to use cURL to get the JSON response from a public available instagram profile. I'm stuck to the response, because If I try to var_dump the data it will result in nothing, a blank string. Is there something wrong in my code?

$url = 'https://instagram.com/'.$instance['username'].'/?__a=1';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Accept: application/json') );
$data = curl_exec($ch);
curl_close($ch);
var_dump( $url );

The $instance variable is set using a wordpress widget input form and is correct. My problems are only with the curl beahviour.

Thanks for the help

Upvotes: 0

Views: 4420

Answers (1)

Hossein Zare
Hossein Zare

Reputation: 580

The script needs a CURLOPT_FOLLOWLOCATION parameter to be set.

$url = 'https://instagram.com/'. $instance['username'] .'/?__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); // Show the response

Upvotes: 2

Related Questions