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=&units=metric&cnt=7&lang=en&key=f38a36af9e4965dd5192ba4282abe070) [function.file-get-contents]:无法打开流:连接在 /xxx/xxx/public_html/fullweather 超时。php 第 15 行

如有任何帮助,我们将不胜感激。 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 刚刚关闭,因为 link 现在正在为我工​​作。 再试一次。

可能 API 已经改变了 url。尝试查看文档以获取任何更新信息。我认为他们可以更改它,但找不到您的代码,因为此 $url 不再存在。

我在 url 中添加了 https 而不是 http 并且有效。

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

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

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