我正在尝试从accuweather API中获取json数据,以获取PHP中的locationKey。输出如下所示:file_get_contents(https://dataservice.accuweather.com/forecasts/v1/daily/1day/55488?apikey=0NSY9T1tFGo0NIXOYp23lro8DsuOcwPJ): failed to open stream: HTTP request failed!
<?php
$url = "https://dataservice.accuweather.com/forecasts/v1/daily/1day/55488?apikey=0NSY9T1tFGo0NIXOYp23lro8DsuOcwPJ";
$json = file_get_contents($url);
$data = json_decode($json,true);
print_r($data);
?>
allow_url_fopen
已使能。
我该怎么办?
**编辑:**我按如下所示更改了代码,但它不起作用
function weather(){
$url='http://dataservice.accuweather.com/forecasts/v1/daily/5day/55488?apikey=0NSY9T1tFGo0NIXOYp23lro8DsuOcwPJ';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl,CURLOPT_FAILONERROR,true);
curl_setopt($curl, CURLOPT_URL, $url);
$result = curl_exec($curl);
$res=(object)array(
'response' => curl_exec( $curl ),
'status' => curl_getinfo( $curl, CURLINFO_RESPONSE_CODE ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
curl_close($curl);
echo 'response:'.$res->response;
echo '<br>';
echo 'status:'.$res->status;
echo '<br>';
echo 'errors:'.$res->errors;
}
结果为具有以下特征的空白页:
response:
status:0
errors:Empty reply from server
2条答案
按热度按时间h4cxqtbf1#
Fetch
api似乎是最简单的解决方案,因为端点确实有Access-Control-Allow-Origin: *
头-下面是一个简单的例子来说明基本原理。第一个
在PHP中,你可以使用
curl
轻松地从AccuWeather API获取响应。下面的curl函数有一个cacert
变量,它指向一个有效的cacert.pem
文件--在协商SSL连接时通常需要这个变量。这就产生了:
at0kjp5o2#