php - 我无法获取特定 URL 的内容
问题描述
我无法获取此 URL 的内容(我得到一个空白页面):https ://www.euronews.com/api/watchlive.json
我一直使用这个功能,从来没有遇到过问题:
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$data = json_decode(file_get_contents_curl("https://www.euronews.com/api/watchlive.json"), true); $url = $data['url']; echo $url;
任何想法?
编辑:Raspbian(Raspberry)中的 cURL 模块(可能)引起的未知问题
解决方案
您在函数响应中得到的超出预期,因此 json_decode 失败。您需要将 CURLOPT_HEADER 设置为 false 而不是 5。
curl_setopt($ch, CURLOPT_HEADER, FALSE);
你可以通过做来验证你得到了什么
$response = file_get_contents_curl("https://www.euronews.com/api/watchlive.json");
var_dump($response);
$data = json_decode($response, true);
$url = $data['url'];
echo $url;
推荐阅读
- javascript - 检测异步函数的“仅返回承诺”状态
- button - 如何确定使用 xPath 选择了哪个选项按钮
- javascript - 为什么我无法访问设置了 src 或 srcdoc 属性的 iframe 文档?
- c# - 我怎么写这个,所以程序在用户输入他的名字后重新启动
- arrays - 在 Hive SQL 中分解列表以识别空白
- html - HTML CSS & Javascript 响应式导航栏
- solrj - 使用 Spring Data Solr 标记字段
- r - 我只能在 R 中安装软件包,只能从桌面打开 R
- java - 使用 Apache Commons CSV 从带有 Header 的 HashMap 列表中写入 CSV
- javascript - Protractor 的 getText() 方法不包括在 ::first-letter 伪选择器中完成的文本转换