Get JSON result with php

1

The code is complete I believe but I do not know how to work with json , I just need to get the result of the other days that I can only get from the code today:

$chave = 'fdhfju35'; // Obtenha sua chave de API gratuitamente em http://hgbrasil.com/weather

// Resgata o IP do usuário
$ip = $_SERVER["REMOTE_ADDR"];

function hg_request($parametros, $chave = null, $endpoint = 'weather'){
  $url = 'http://api.hgbrasil.com/'.$endpoint.'/?format=json&';

  if(is_array($parametros)){
    // Insere a chave nos parametros
    if(!empty($chave)) $parametros = array_merge($parametros, array('key' => $chave));

    // Transforma os parametros em URL
    foreach($parametros as $key => $value){
      if(empty($value)) continue;
      $url .= $key.'='.urlencode($value).'&';
    }

    // Obtem os dados da API
    $resposta = file_get_contents(substr($url, 0, -1));

    return json_decode($resposta, true);
  } else {
    return false;
  }
}
 $dados = hg_request(array(
   'user_ip' => $ip
 ), $chave);
 function imagem($valor){
    $pontos = array("n");
    $result = str_replace($pontos, "", $valor);
    return $result;
}

With this code, I can get the temperature for today and the other days how do I do it?

$dados['results']['temp'] and in command json provides the other days just do not know how to get: link

    
asked by anonymous 21.11.2016 / 13:30

2 answers

3

That's what you want, the array $dados['results']['forecast'] with every day:

$raw = file_get_contents('http://api.hgbrasil.com/weather?format=json&');
$dados = json_decode($raw, true);
echo '<pre>', print_r($dados['results']['forecast']), '</pre>';

You can print one by one:

$raw = file_get_contents('http://api.hgbrasil.com/weather?format=json&');
$dados = json_decode($raw, true);
foreach($dados['results']['forecast'] as $dia) {
    echo 'Dia: ' .$dia['date']. '<br>';
    echo 'Max: ' .$dia['max']. '<br>';
    echo 'Min: ' .$dia['min']. '<br><br>';
}
    
21.11.2016 / 13:51
1

The key is ['results']['forecast'] , I made a complete example with all the keys including what I needed most.

<?php

// Função responsável em trazer os dados json
function getForecast()
{
    $url  = 'http://api.hgbrasil.com/weather?format=json&';
    $json = file_get_contents($url);
    return json_decode($json, true);
}

// Variável que recebe os dados no formato array
$result = getForecast();

// Imprimindo os dados
printf('<p>%s</p>', $result['by']);
printf('<p>%s</p>', $result['valid_key']);
printf('<p>%s</p>', $result['results']['temp']);
printf('<p>%s</p>', $result['results']['date']);
printf('<p>%s</p>', $result['results']['time']);
printf('<p>%s</p>', $result['results']['condition_code']);
printf('<p>%s</p>', $result['results']['description']);
printf('<p>%s</p>', $result['results']['currently']);
printf('<p>%s</p>', $result['results']['cid']);
printf('<p>%s</p>', $result['results']['city']);
printf('<p>%s</p>', $result['results']['img_id']);
printf('<p>%s</p>', $result['results']['humidity']);
printf('<p>%s</p>', $result['results']['wind_speedy']);
printf('<p>%s</p>', $result['results']['sunrise']);
printf('<p>%s</p>', $result['results']['sunset']);
printf('<p>%s</p>', $result['results']['condition_slug']);
printf('<p>%s</p>', $result['results']['city_name']);

// todas as datas...
foreach ($result['results']['forecast'] as $value)
{
    printf('<p>%s</p>', $value['date']);
    printf('<p>%s</p>', $value['weekday']);
    printf('<p>%s</p>', $value['max']);
    printf('<p>%s</p>', $value['min']);
    printf('<p>%s</p>', $value['description']);
    printf('<p>%s</p>', $value['condition']);
    echo '<hr />';
}

printf('<p>%s</p>', $result['execution_time']);
printf('<p>%s</p>', (string)$result['from_cache'] == 1 ?  "true": "false");
    
21.11.2016 / 13:51