首页 > 解决方案 > 我无法获取特定 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 模块(可能)引起的未知问题

标签: php

解决方案


您在函数响应中得到的超出预期,因此 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;

推荐阅读