curl未按预期返回JSON数据

lo8azlld  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(168)

我有下面的代码,在这里我基本上只是执行一个curl请求来取回JSON数据..请求的URL应该是:

https://www.instagram.com/reel/CiAbVWmpiWR/?__a=1&__d=dis

因此,即使我只是将上面的链接复制并粘贴到浏览器中,我也可以看到JSON数据,但是当将其放入代码并通过CURL请求时..它只是返回哎呀,发生了一个错误..我做错了什么?

$mediaUrl = 'https://www.instagram.com/reel/CiAbVWmpiWR';
$pos = strpos($mediaUrl, '?');
if ($pos) {
    $mediaUrl = substr($mediaUrl, 0, $pos);
}
$url = rtrim($mediaUrl, '/') . '/?__a=1&__d=dis';

//var_dump($url);

$proxy = '139.99.54.49:10163';
$proxyauth = 'aditya:xcQWzyfX7ybNM8d';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);     // PROXY details with port
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   // Use if proxy have username and password
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);
if (curl_errno($ch)) {
    $error_msg = curl_error($ch);
    var_dump($error_msg);
}

$json_data = json_decode($data, true);
var_dump($data);

因此var_dump数据返回

string(25) "Oops, an error occurred.

curl_errno($ch) is always NULL

更新日期:

当URL不是Instagram卷轴照片时,仅为常规照片,如以下示例所示:
https://www.instagram.com/p/CiPR10FhwZ6/?__a=1&__d=dis
上面的代码运行得非常好

ffx8fchx

ffx8fchx1#

我认为返回的API是GraphQl格式的。您能否考虑以下代码并参考有关GraphQL API here的更多信息

$mediaUrl = 'https://www.instagram.com/reel/CiAbVWmpiWR';
        $pos = strpos($mediaUrl, '?');
        if ($pos) {
            $mediaUrl = substr($mediaUrl, 0, $pos);
        }
        $url = rtrim($mediaUrl, '/') . '/?__a=1&__d=dis';
        $endpoint = $url;
        $query = "query {
  
            }";
        $data = array('query' => $query);
        $data = http_build_query($data);
        $options = array(
            'http' => array(
                'header' => "Content-Type: application/json\r\n".
                    "Content-Length: ".strlen($query)."\r\n".
                    "User-Agent:MyAgent/1.0\r\n",
                'method' => 'GET',
                'content' => $data
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($endpoint, false, $context);

        if ($result === FALSE) { /* Handle error */
        }
        print_r($result);

相关问题