Google’s Weather API in PHP

Sharing is caring!

It might be a delightful small task to compose an excercise on the most proficient method to construct an essential yet capable climate provision for either portable units or the desktop. The requisition takes a set of Google climate Api encourages and settles on some straightforward choices dependent upon the yield. For instance we can propose the client takes an umbrella if downpour or storms are gauge, or if the temperature warrants the requirement for warm apparel. Essential however in any case suitable informative data.

Code

Lets have a snappy go through at what the undertaking comprises of. Firstly we utilize the simplexml_load_file() Php capacity to get the xml information for the given area. We then verify whether the present climate condition is in the Xml record. Provided that it doesn’t exist then we can make the surmise that the Api food would be unable to confirm the area, acknowledge this is extremely fundamental mistake checking and if the food founders it will even now return this mistake.

We now do some fundamental information verifying what additional information we can give the client. Firstly we wiretap line 14 to check whether the present climate and estimate are distinctive, in the event that they are diverse we’ll show the estimate if not we demonstrate just the present climate.

Afterward on lines 21 and 28 we do a straightforward verify whether the temperature is higher or lower than 20 degrees and reverberation if its prone to be blazing or cool.

At last on lines 35 we verify whether the present or future figure holds any drizzle or storms so we can propose the client takes an umbrella. This is finished utilizing some fundamental Php customary declaration.

———————————————————————————————————————-

// load the XML feeds for the Google Weather API
$xml = simplexml_load_file(‘http://www.google.com/ig/api?weather=’.urlencode($_GET[‘location’]));
$current = $xml->xpath(“/xml_api_reply/weather/current_conditions”);
$forecast = $xml->xpath(“/xml_api_reply/weather/forecast_conditions”);

// do a basic error check to see if we can get the current weather condition for the given location
// if no return an error.
if(!$current[0]->condition[‘data’]){

$error = ‘Couldn’t determine this location’;
}

// is the current weather the same as the forecast? if not display the forecast
if(strtolower($current[0]->condition[‘data’])!=strtolower($forecast[0]->condition[‘data’])){

$outlook = ‘but the forecast says ‘.strtolower($forecast[0]->condition[‘data’]);
}

// if the temp in degrees c is below 20 i.e. cold
if($current[0]->temp_c[‘data’]<=20){

$coat = ‘If you’re going outside i’d wrap up warm.’;
}

// if the temp in degrees c is over 21 i.e. Warm / Hot
if($current[0]->temp_c[‘data’]>=21){

$coat = ‘You should be ok without warm clothes today.’;

}

// check to see if there is rain or storms forecast
if (preg_match(“/brainb/i”, $current[0]->condition[‘data’]) ||
preg_match(“/brainb/i”, $forecast[0]->condition[‘data’]) ||
preg_match(“/bstormb/i”, $current[0]->condition[‘data’]) ||
preg_match(“/bstormb/i”, $forecast[0]->condition[‘data’])
){
$umbrella = ‘ But <u>don’t forget to take an umbrella</u>!’;
}

Displaying data

<form action=”” method=”get”>
<label for=”location”>Location</label>
<input type=”text” class=”location” name=”location” value=”<?php echo $_GET[‘location’];?>”/>
</form>

<?php if(!empty($_GET[‘location’])){

if($error){ echo ‘

‘.$error.’

‘; }else{

?>

Weather Summary for

  • icon[‘data’].'”/>’; ?> The weather in is condition[‘data’]).’ ‘. $outlook;?>. The temperature is currently temp_c[‘data’]; ?>°c (temp_f[‘data’]; ?>°f).

<?php } } ?>

2 thoughts on “Google’s Weather API in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange