PHP JSON 响应未从 URL 加载

以下是提供 JSON 响应的链接:https://www.instagram.com/p/CDq1KJPJcTN/ ?__a=1


简单地将 URL 复制并粘贴到浏览器中即可得到 JSON 响应。但是当我尝试使用 file_get_contents 或 cURL 在 PHP 中访问相同内容时,它显示 Instagram 的 html 页面代码


ini_set("allow_url_fopen", 1);

ini_set("allow_url_include", 1);


$context = [

            'http' => [

                    'method' => 'GET',

                    'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36',

                ],

            ];

$context = stream_context_create($context);

$data = file_get_contents('https://www.instagram.com/p/CDq1KJPJcTN/?__a=1', false, $context);

卷曲:


$curl_handle=curl_init();

curl_setopt($curl_handle, CURLOPT_URL,'https://www.instagram.com/p/CDq1KJPJcTN/?__a=1');

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36');

$query = curl_exec($curl_handle);

curl_close($curl_handle);

echo $query;

当我使用rest api测试工具(https://resttesttest.com/)时。它仅给出 JSON 响应。我认为缺少一些重要的标题,我无法弄清楚。给出了 Content-Type:application/json 和 Access-Control-Allow-Origin: * 。


凤凰求蛊
浏览 93回答 1
1回答

扬帆大鱼

尝试这个$curl_handle=curl_init();curl_setopt($curl_handle, CURLOPT_URL,'https://www.instagram.com/p/CDq1KJPJcTN/?__a=1');curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);curl_setopt($curl_handle, CURLOPT_HEADER, FALSE);curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36');curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);$query = curl_exec($curl_handle);curl_close($curl_handle);echo $query;使用 file_get_contents 在本地对我有用ini_set("allow_url_fopen", TRUE);ini_set("allow_url_include", TRUE);$context = [            'http' => [                    'method' => 'GET',                    'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36',                ],            ];$context = stream_context_create($context);$data = file_get_contents('https://www.instagram.com/p/CDq1KJPJcTN/?__a=1', false, $context);echo $data;
打开App,查看更多内容
随时随地看视频慕课网APP