Weather forecast script in php using Google Weather API


This method no longer works since google now no longer allows public access to this api.
Use the following JSON weather API instead:
Weather forecast using javascript and free api

A few days back , we published an article about currency conversion using Google calculator API and php. Today we are going to show you how to display current weather information and forecast data using php and Google Weather API.
The API is situated at and the callback URL for a specific place looks like this :


The returned data in XML form contains information such as current temperature , weather condition , humidity , winds and forecast for the next few days. The following is a simple php script which extracts the current temperature, weather condition, humidity and temperature & weather condition forecast for one day. The following example uses CURL so you must have it enabled for the code to work. ( Note : The following code displays temperature in °C )

//Name of your town/city

//Initialize CURL
$ch = curl_init();

$timeout = 0;

//Set CURL options
curl_setopt ($ch, CURLOPT_URL, ''.$place.'&hl=en');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);


//close CURL cause we dont need it anymore

	// Parse the XML response
	$xml = new SimplexmlElement($xml_str);
	foreach($xml->weather as $item) {

		foreach($item->current_conditions as $new) {
                        //For temperature in fahrenheit replace temp_c by temp_f


                //to convert Fahrenheit into Celcius



Assign the required value to the $place variable and place the code in your file. The script assigns 5 variables : $current_temperature, $current_condition, $current_humidity, $next_temperature, $next_condition which you can use as following :

Current temperature : <?php echo $current_temperature; ?> &degC <br />
Current condition : <?php echo $current_condition; ?> <br />
<?php echo $current_humidity; ?> <br />
Tomorrows temperature : <?php echo $next_temperature; ?> &degC <br />
Tomorrows condition : <?php echo $next_condition; ?> <br />

5 responses to this entry

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2020 All rights reserved | Powered by WordPress
Back to top
Theme by