问题描述
我正在使用https://www.instagram.com/username/?__a=1来检索用户ID。每当我在浏览器中使用该URL时,它都会返回正确的JSON,但是当我在PHP中使用“获取文件内容”或“ curl”进入该URL时,它将返回完整的HTML页面。该URL无需在浏览器中登录就可以正常工作,所以这不是问题。
解决方法
使用file_get_contents
。
$url = "https://www.instagram.com/username/?__a=1";
$response = file_get_contents($url);
使用curl
$url = "https://www.instagram.com/username/?__a=1";
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
在两种情况下,$response
均包含有效的JSON字符串。