PHP Get current temperature for a zip code

Leverages Google's weather data API to fetch the current temperature for a given zip/postal code.
$httpCall = 'www.google.com/ig/api?weather=' . $postal;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $httpCall);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
	
if (strpos($output, 'problem_cause') === false) {
	$xml = new SimpleXMLElement($output);
	$weather = $xml[0]->weather->current_conditions;
	echo 'Current Temperature: ' . $weather->temp_f;
}
Snippet Viewed 9444 times.

Share your PHP code snippets:

  • Get some recognition & link back to your site.

 

Submit | Browse

Most Recent php snippets

Most Viewed