亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP JSON 響應未從 URL 加載

PHP JSON 響應未從 URL 加載

PHP
鳳凰求蠱 2023-09-22 16:37:33
以下是提供 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: * 。
查看完整描述

1 回答

?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

嘗試這個


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


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號