Extract json information in php

Asked

Viewed 229 times

-1

Guys I have the following code:

<?php

$key = "*****************";
$forcast_days='5';
$city = '-30.1087957,-51.3169879';
$url ="http://api.apixu.com/v1/forecast.json?key=$key&q=$city&days=$forcast_days&=";

$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$json_output=curl_exec($ch);
$weather = json_decode($json_output);


$days = $weather->forecast->forecastday;

echo "Cidade: ". $weather->location->name;
echo "<br>";
echo "Estado: ".$weather->location->region;
echo "<br>";
echo "Pais: ".$weather->location->country;

foreach ($days as $day){


echo "<table>";    
    echo "<tr><td colspan='4' border='0'><h2>{$day->date}</h2>";

    echo "<tr><td><h4>Wind</h4></td><td colspan='3'>{$day->day->maxwind_mph}Mph <br> {$day->day->maxwind_kph}kph </td></tr>";
    foreach ($day->hour as $hr){

        echo "<tr><td colspan='4' border='0'>";
        echo "<table style='width:100%;'>";    

        echo "<tr><td>Time</td><td>Temprature</td><td>Wind</td><td>Humidity</td></tr>";
        echo "<tr><td><div>{$hr->time}<img src=' {$hr->condition->icon}'/></div></td><td>{$hr->temp_c}&deg;C<br>{$hr->temp_f}&deg;F</td><td>{$hr->wind_mph}Mph <br> {$hr->wind_kph}kph</td><td>$hr->humidity</td></tr>";

        echo "</table></tr></td>";
    }
echo "</table> <br>";        

Here is the result on the web: http://ioenergias.com.br/climapesca

How I can select variables, for example, from 00:00 to 06:00 only?

  • 2

    You can put an example of the pure json you receive sff?

  • JSON is not offered. But you have the API Docs page: https://www.apixu.com/doc/forecast.aspx

  • I’m trying to use some kind of if, for when the time between 00:00 and 06:00 it return the information, more so far without success.

  • @Gustavocave just give one echoor var_dump in the $json_output for you to see what was returned. Then you can post here to be helped.

  • Friend, we need the JSON return even to be able to help you better. But as a hint, I would make this page in Javascript and html. Easier to handle converted JSON array in Javascript.

  • I tried var_dump(json_decode($json_output, true)); and it returns NULL

Show 1 more comment

1 answer

1

That would work:

<?php

$key = '***************************';
$forcast_days='5';
$city = '-30.1087957,-51.3169879';
$url ="http://api.apixu.com/v1/forecast.json?key=$key&q=$city&days=$forcast_days&=";

$ch = curl_init();  
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$json_output=curl_exec($ch);
$weather = json_decode($json_output);

$days = $weather->forecast->forecastday;

foreach($days as $day) {
    echo "<h1>".date('d/m/Y', strtotime($day->date))."</h1>";

    foreach($day->hour as $hour) {
        $time = explode(' ', $hour->time);
        $time = $time[1];
        $time = explode(':', $time);
        $time = $time[0];

        if($time >= 0 and $time <= 6) {
            echo "As {$time}:00h teremos {$hour->temp_c}Cº<br>";
        }
    }
    echo '<hr>';
}

On the return, I took the days that were selected, I went through this list of days, each element had a title with the date of the day, and within that day I went through his hours, on the return I noticed that $hour->time would give a date and time in the format 2017-01-02 00:00 so I cut that string in the space( ) and then in the two points(:), then the resulting time of day, so it was easy to make the comparison.

  • Yes, it works, but it got complicated to treat the rest of the Infos, for example, I will need an average temperature between those times to then show some information.

  • Should have explained in the question.

  • 1

    And it does not complicate, just go incrementing a variable with the value of the temperature in each hour, and in the end divide by the amount of hour. Tip: improve your logic.

  • @Ivcs Ctz, need to improve logic and programming, I am amateur.

Browser other questions tagged

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