Search code examples
phpjsonapiparsingweather-api

Regarding JSON parsing error


<?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


Solution

  • 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.