-2
<?php
$url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=YOURAPI";
$json    = file_get_contents($url);
$data    = json_decode($json, true);
$data['city']['name'];

foreach ($data['list'] as $day => $value) {
  echo $todaystemperature = $value[temp][max];
}
?>

This has been working suddenly stopped working some reason. I keep getting on file_get_contents. Not sure what make this code mess

Eurasia
  • 5
  • 5
  • Well running CURL from command line brings back `{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."` where as a browser brings back a response so perhaps they change their connections requirements, or your key hit some limit when not being used from a browser. (Also if that is a key specific to you you should modify/remove it) – chris85 Oct 13 '16 at 17:01
  • @Eurasia. I have provided with the result. Have a check and let me know about your expectations. – Naresh Kumar P Oct 13 '16 at 17:27
  • Thank you for these steps.. much appreciated – Eurasia Oct 13 '16 at 17:36

4 Answers4

0

your code seems not right, the url you are using is giving following response

    {
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 804,
      "main": "Clouds",
      "description": "overcast clouds",
      "icon": "04d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 284.66,
    "pressure": 1016.88,
    "humidity": 68,
    "temp_min": 284.66,
    "temp_max": 284.66,
    "sea_level": 1024.55,
    "grnd_level": 1016.88
  },
  "wind": {
    "speed": 4.91,
    "deg": 97.501
  },
  "clouds": {
    "all": 92
  },
  "dt": 1476377646,
  "sys": {
    "message": 0.0048,
    "country": "GB",
    "sunrise": 1476339772,
    "sunset": 1476378553
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

In the above json response i can not find any key with 'list' that is why your code breaks. This code should be fine to get the temp of the city

<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp'];
?>
axcl
  • 410
  • 6
  • 21
  • I don't know... It may problem with my browser.. I get Warning: file_get_contents(http://api.openweathermap.org/data/2.5/weather? q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/pray4mal/public_html/pray-dev/weather/weather.php on line 27 – Eurasia Oct 13 '16 at 17:16
  • On line $json = file_get_contents($url); – Eurasia Oct 13 '16 at 17:16
  • can you access it in your browser try hitting it in your browser – axcl Oct 13 '16 at 17:17
  • it is working but it seems the data is wrong 284.66.. I am asking help from api.openweathermap – Eurasia Oct 13 '16 at 17:19
  • to convert it to Celsius $data['main']['temp'] – 273.15 – axcl Oct 13 '16 at 17:25
0

The JSON Data needs to be parsed based on the JSON response that you are getting.

Steps for Getting the Json Data using PHP

  • First you have to get the data has the json_response.
  • Then you have to make the json response to decode through the json code using json_decode($json,TRUE)
  • After that you can use foreach() for splitting up of the array that you get based on the decoded value.

While running the above URL that you have given in the code i am getting the output as follows.

Obtained JSON:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{"speed":4.91,"deg":97.501},"clouds":{"all":92},"dt":1476377768,"sys":{"message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200}

Hence after following up the process you can get the values like this.

PHP Code:

<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?  q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp']; // To convert the data to Celsius by hitting the API called api.openweathermap. 
?>

Note: $data['main']['temp'] - Make sure that you get data over here and then you will succeed over here.

Output for the Json Parse is as follows for the above obtained JSON:

String Parse:

{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}

Js Eval:

{

    "coord":{
        "lon":-0.13,"lat":51.51},"weather":[
        {
            "id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{
        "temp":284.66,"pressure":1016.88,"humidity":68,"temp_min":284.66,"temp_max":284.66,"sea_level":1024.55,"grnd_level":1016.88},"wind":{
        "speed":4.91,"deg":97.501},"clouds":{
        "all":92},"dt":1476377768,"sys":{
        "message":0.1769,"country":"GB","sunrise":1476339772,"sunset":1476378552},"id":2643743,"name":"London","cod":200

}

Hence when you do the json_decode() for the above obtained JSON string you have the out put as follows.

array (
  'coord' => 
  array (
    'lon' => -0.13000000000000000444089209850062616169452667236328125,
    'lat' => 51.50999999999999801048033987171947956085205078125,
  ),
  'weather' => 
  array (
    0 => 
    array (
      'id' => 804,
      'main' => 'Clouds',
      'description' => 'overcast clouds',
      'icon' => '04d',
    ),
  ),
  'base' => 'stations',
  'main' => 
  array (
    'temp' => 284.66000000000002501110429875552654266357421875,
    'pressure' => 1016.8799999999999954525264911353588104248046875,
    'humidity' => 68,
    'temp_min' => 284.66000000000002501110429875552654266357421875,
    'temp_max' => 284.66000000000002501110429875552654266357421875,
    'sea_level' => 1024.549999999999954525264911353588104248046875,
    'grnd_level' => 1016.8799999999999954525264911353588104248046875,
  ),
  'wind' => 
  array (
    'speed' => 4.910000000000000142108547152020037174224853515625,
    'deg' => 97.501000000000004774847184307873249053955078125,
  ),
  'clouds' => 
  array (
    'all' => 92,
  ),
  'dt' => 1476377768,
  'sys' => 
  array (
    'message' => 0.176900000000000001687538997430237941443920135498046875,
    'country' => 'GB',
    'sunrise' => 1476339772,
    'sunset' => 1476378552,
  ),
  'id' => 2643743,
  'name' => 'London',
  'cod' => 200,
)

Hence after getting the array it seems to be of Single Object with array for some of the keys that has been obtained.

enter image description here

Hence you can fetch the data only from this output obtained using the loop operators called foreach(). Hence by using the foreach() the array data will be coming in the loop as single manner and then it will manipulate the result.

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
-1
    $json = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=a1fc2c19d548237a56e0edd7b79b3ebc');

    $data = json_decode($json,true);
    OR
   $data = json_decode($json);
    echo "<pre>";

    print_r($data);

    exit;
-1

Here is the result

<?php
 $url     = "http://api.openweathermap.org/data/2.5/weather?q=London,uk&units=imperial&cnt=1&lang=en&appid=YOURAPI";
 $json    = file_get_contents($url);
 $data    = json_decode($json, true);
 echo $temp = $data['main']['temp'];
?>
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Eurasia
  • 5
  • 5