cURL返回null

问题描述 投票:0回答:1

我一直使用这段代码来检索用户的Instagram个人资料照片:

$curl = curl_init("https://www.instagram.com/{$_SESSION['instagram']['user']}/?__a=1");

curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$result = json_decode(curl_exec($curl), true);
curl_close($curl);

$_SESSION['instagram']['picture'] = $result['graphql']['user']['profile_pic_url_hd'];

响应:

注意:尝试在**行的*******中访问类型为null的值的数组偏移量

[$_SESSION['instagram']['user']来自API调用,并将其回显,我得到了正确的值。

即使用Instagram用户名手动替换$_SESSION['instagram']['user'],我也得到相同的结果,而在谷歌上搜索相同的URL时,我得到了正确的JSON响应。

same代码始终有效,因此我想到了Instagram错误或因请求过多而暂时被“禁令”。

但是几天后,什么都没有改变。

有什么想法吗?

更新:curl_setopt($url, CURLOPT_URL, "https://www.instagram.com/{$_SESSION['instagram']['user']}/?__a=1")产生相同的结果

php json api curl instagram
1个回答
0
投票

下面的代码段可以很好地使用我的用户名,

<?php
$username = $_SESSION['instagram']['user'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.instagram.com/$username/?__a=1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = json_decode(curl_exec($ch),true);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result['graphql']['user']['profile_pic_url_hd'];
?>
© www.soinside.com 2019 - 2024. All rights reserved.