PHP[function.file-get-contents]:无法打开流


PHP [function.file-get-contents]: Failed to open stream

这真的很烦人,php脚本正在工作,并从打开的天气图中返回天气,但突然之间,php脚本需要很长时间才能执行,并产生以下错误:

警告:file_get_contents(http://api.openweathermap.org/data/2.5/weather?lat=&lon=&单位=公制&cnt=7&lang=en&key=f38a36af9e4965dd5192b4282abe070)[function.file-get-contents]:无法打开流:第15行的/xxx/xxx/public_html/fullweather.php中的连接超时

如有任何帮助,我们将不胜感激。php脚本可以在下面查看。只有开头很重要(前18行),但我已经包含了全部内容。

 <?php

$lat = $_POST["latitude"];
$lon = $_POST["longitude"];
$url="http://api.openweathermap.org/data/2.5/weather?lat=".$lat."&lon=".$lon."&units=metric&cnt=7&lang=en&key=f38a36af9e4965dd5192ba4282abe070";

// Make call with cURL
$session = curl_init($url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
$json=file_get_contents($url);
$data=json_decode($json,true);
class weatherdata
{
            public $temp = "";
            public $weather = "";
            public $clouds = "";
            public $area = "";
            public $humidity = "";
            public $humidpressure = "";
            public $weatherdesc = "";
            public $tempmin = "";
            public $tempmax = "";
            public $windspeed = "";
            public $winddirec = "";
 }
$data1 = $data['main']['temp'];
$data2 = $data['weather'][0]['main'];
$data3 = $data['clouds']['all'];
$data4 = $data['name'];
$data5 = $data['main']['humidity'];
$data6 = $data['main']['pressure'];
$data7 = $data['weather'][0]['description'];
$data8 = $data['main']['temp_min'];
$data9 = $data['main']['temp_max'];
$data10 = $data['wind']['speed'];
$data12 = $data['wind']['deg'];


$weatherdata = new weatherdata();
$weatherdata->temp = $data1;
$weatherdata->weather = $data2;
$weatherdata->clouds = $data3;
$weatherdata->area = $data4;
$weatherdata->humidity = $data5;
$weatherdata->humidpressure = $data6;
$weatherdata->weatherdesc = $data7;
$weatherdata->tempmin = $data8;
$weatherdata->tempmax = $data9;
$weatherdata->windspeed = $data10;
$weatherdata->winddirec = $data12;

$output[] = $weatherdata;
print(json_encode($output));
?>

我认为API刚刚关闭,因为链接现在对我有效。再试一次。

可能是API更改了它的url。请尝试查看文档中的任何更新信息。我认为他们可以更改它,但你的代码找不到,因为这个$url已经不存在了。

我在url中添加了https而不是http,它起到了作用。

所以也许可以像这个url一样更改url中的http。

$url="https://api.openweathermap.org/data/2.5/weather?lat=".$lat."&lon=".$lon."&units=metric&cnt=7&lang=en&key=f38a36af9e4965dd5192ba4282abe070";

编辑此外,您不应该向任何人显示api密钥,因为有些人可以使用它。