Get data from Json by php

-1

People need to get data from a JSON file (I'm not the fuck in the business). This api below is for displaying temperature via JSON + PHP, remembering that on the same site I can not get the maximum, minimum or display days of the week, but this data is in JSON, but how do I 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 it to you how I can display the other values.

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

More below you can see the JSON file formatted to understand better and also his link Online

link

{  
   "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
}
    
asked by anonymous 12.04.2017 / 15:24

2 answers

2

If you want to catch the minimum and maximum of today do it!

//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 turn this into an array you can use: array_search('value(dia)', $array) to search for the day you want

    
12.04.2017 / 15:45
1

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

$dados->results->city_name;

Here's an example of Json in the sandbox.

    
12.04.2017 / 15:52