首页 > 解决方案 > 使用 cURL PHP 获取 Instagram 公共 JSON 提要

问题描述

我想使用 cURL 从公共可用的 Instagram 配置文件中获取 JSON 响应。我坚持响应,因为如果我尝试处理var_dump数据,它将什么都没有,一个空白字符串。我的代码有问题吗?

$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 );

$instance变量是使用 wordpress 小部件输入表单设置的,并且是正确的。我的问题只在于 curl 行为。

谢谢您的帮助

标签: phpcurl

解决方案


该脚本需要CURLOPT_FOLLOWLOCATION设置一个参数。

$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

推荐阅读