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

252

This is really annoying me, the php script was working and returning the weather from open weather map but literally all of a sudden the php script takes forever to execute and the following error is produced:

Warning: 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]: failed to open stream: Connection timed out in /xxx/xxx/public_html/fullweather.php on line 15

Any help would be appreciated. The php script can be viewed below. Only the beginning is important (first 18 lines) but I have included the full thing.

 <?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));
?>
868

Answer

Solution:

I think the API was just down, cause the link is working for me right now. Try one more time.

870

Answer

Solution:

May be the API had change it's url by the time. Try to check the documentation for any updated information. I think that they could change it and your code couldnt find because this$url doesn't exist anymore.

170

Answer

Solution:

i addedhttps instead ofhttp in url and it worked.

so maybe change http in url like this url.

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

Edit Also you should not show your api key to anyone as some one can use it.

People are also looking for solutions to the problem: javascript - FormData object always empty

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.