Get Json data from php

Asked

Viewed 763 times

-1

People need to get data from a JSON file (I’m not the fuck in business). This api below is to display temperature via JSON + PHP, remembering that on the site of the same I can not get the maximum, minimum or display days of the week, but these data are in JSON, but how I will display them ?

    <?php

    /*
     * Obtendo dados do HG Weather API
     *
     * Consulte nossa documentacao em http://hgbrasil.com/weather
     * Contato: [email protected]
     * Copyright 2015 - Hugo Demiglio - @hugodemiglio
     *
    */

    $cid = 'BRXX0198'; // CID da sua cidade, encontre a sua em http://hgbrasil.com/weather

    $dados = json_decode(file_get_contents('http://api.hgbrasil.com/weather/?cid='.$cid.'&format=json'), true); 
// Recebe os dados da API

    ?>

Below I’ll leave how I can display the other values.

<?php echo $dados['results']['city_name']; ?>
<?php echo $dados['results']['temp']; ?>

Below you can see the formatted JSON file to better understand and also its link Online

https://api.hgbrasil.com/weather/? format=json&cid=BRXX0198

{  
   "by":"cid",
   "valid_key":false,
   "results":{  
      "temp":23,
      "date":"12/04/2017",
      "time":"09:11",
      "condition_code":"32",
      "description":"Ensolarado",
      "currently":"dia",
      "cid":"",
      "city":"Ribeirao Preto,",
      "img_id":"32",
      "humidity":"76",
      "wind_speedy":"11.27 km/h",
      "sunrise":"6:22 am",
      "sunset":"6:2 pm",
      "condition_slug":"clear_day",
      "city_name":"Ribeirao Preto",
      "forecast":[  
         {  
            "date":"12/04",
            "weekday":"Qua",
            "max":"28",
            "min":"20",
            "description":"Tempestades",
            "condition":"storm"
         },
         {  
            "date":"13/04",
            "weekday":"Qui",
            "max":"28",
            "min":"18",
            "description":"Parcialmente nublado",
            "condition":"cloudly_day"
         },
         {  
            "date":"14/04",
            "weekday":"Sex",
            "max":"29",
            "min":"18",
            "description":"Tempestades isoladas",
            "condition":"storm"
         },
         {  
            "date":"15/04",
            "weekday":"Sáb",
            "max":"30",
            "min":"18",
            "description":"Ensolarado com muitas nuvens",
            "condition":"cloudly_day"
         },
         {  
            "date":"16/04",
            "weekday":"Dom",
            "max":"30",
            "min":"18",
            "description":"Parcialmente nublado",
            "condition":"cloudly_day"
         },
         {  
            "date":"17/04",
            "weekday":"Seg",
            "max":"26",
            "min":"18",
            "description":"Tempestades isoladas",
            "condition":"storm"
         },
         {  
            "date":"18/04",
            "weekday":"Ter",
            "max":"28",
            "min":"19",
            "description":"Tempo nublado",
            "condition":"cloud"
         },
         {  
            "date":"19/04",
            "weekday":"Qua",
            "max":"27",
            "min":"20",
            "description":"Tempestades",
            "condition":"storm"
         },
         {  
            "date":"20/04",
            "weekday":"Qui",
            "max":"21",
            "min":"19",
            "description":"Tempestades isoladas",
            "condition":"storm"
         },
         {  
            "date":"21/04",
            "weekday":"Sex",
            "max":"22",
            "min":"16",
            "description":"Tempestades isoladas",
            "condition":"storm"
         }
      ]
   },
   "execution_time":0.0,
   "from_cache":true
}
  • I do not understand very well what you want. You want to display in your php the day of the week, min temp and max?

  • Exact would like to take the minimum, maximum and the day of the week. I don’t know much of JSON, like tried in the following ways. &#xA;<?php echo $dados['results']['max']; ?>&#xA;<?php echo $dados['results']['forecast']['max']; ?>&#xA;<?php echo $dados['forecast']['max']; ?>&#xA;

  • 1

    No json { means object and [ array, then to access this information you will have to go through the forecast array. Example: <?php echo $dados['results']['forecast'][0]['max']; ?> <?php echo $dados['results']['forecast'][0]['min']; ?> <?php echo $dados['results']['forecast'][0]['weekday']; ?>

  • I did a test going through the array and really returned me the data of it very straight ! Thank you so helped a lot.

2 answers

2


If you want to get the minimum and maximum today do this!

//para pegar as temperaturas use o objeto: $obj->results->forecast

$json = file_get_contents('http://api.hgbrasil.com/weather/?cid=BRXX0198&format=json');
$obj = json_decode($json);

foreach($obj->results->forecast as $k):
    if(date('d/m', mktime(0,0,0,date('m'), date('d'), date('Y'))) == $k->date):
        echo $k->date.' - '.$k->weekday.' - '.$k->min.' - '.$k->max;
    endif;
endforeach;

If you can transform this into an array you can use: array_search('value(dia)', $array) to find the day you want

  • Correct, that way also returned me the data correctly, if I do the @Jessika way will also work, I am grateful to have also helped.

  • If I helped, upvote :D, now that you have the notion of how to go through the loop, you can create your own codes.

  • Since I have the current day data running through the array, is there a way I can display the data for the next 3 days ? Today is Thursday Here I display the data of the day, temp, max, ?

  • 1

    On parole if(date('d/m'....) you realize that there is a function date('d') she’s inside the mktime(), just insert two more if's thus: if(date('d/m'), mktime(0,0,0,date('m), date('d) + 1), date('Y)) //essa condicional pega o dia de amanhã if(date(), mktime(0,0,0,date('m), date('d) + 2), date('Y)) //essa condicional pega o dia depois de amanha, observe the date('d')+1 //significa dia de hoje mais 1 and the other is worth the same.

1

json_decode transforms json into a std_class object, that is, to retrieve the desired information use as follows:

$dados->results->city_name;

Follows here an example of Json in sandbox.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.