Get JSON result with php

Asked

Viewed 1,769 times

1

the code is complete I believe but I do not know work with json, I just need to get the results from the other days that I can only get from today’s code:

$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 but the other days as I do?

$dados['results']['temp'] and in charge json provides the other days just don’t know how to pick up: http://api.hgbrasil.com/weather?format=json&

  • You want to list every day ? You will have to trace a repetitive loop there.

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>';

That to print one by one can:

$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>';
}
  • Thank you so much That’s just what I wanted more!

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");
  • 1

    vlw guy also helped me a lot I’ll have much more than I wanted to vlw even

Browser other questions tagged

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