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

时间:2015-03-30 20:15:00

标签: php curl file-get-contents

这真让我烦恼,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]:无法打开流:第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));
?>

3 个答案:

答案 0 :(得分:2)

我认为API刚刚关闭,因为链接现在正在为我工​​作。 再试一次。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我在 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 密钥,因为有人可以使用它。