Php obtains the current google weather implementation program

Source: Internet
Author: User
We will see that many websites can display the local weather in real time. let me tell you how to do this in real time, you can use the googleaip interface to obtain the weather conditions of different cities and display them on your website. Se. phpnbsp...

We will see that many websites can display the local weather in real time. let me tell you how to do this in real time, you can use the google aip interface to obtain the weather conditions of different cities and display them on your website.

Se. php

The code is as follows:

$ City = $ _ GET ['city'];
$ Data = createXml ($ city );

$ Xml = simplexml_load_string ($ data );
Header ("Content-type: text/xml ");
Echo $ xml-> asXML ();

// Generate XML data
Function createXml ($ city)
{
// Google Weather API
$ Weather = simplexml_load_file ("http://www.google.com/ig/api? Weather = {$ city }");
If (isset ($ weather-> forecast_conditions ))
{
$ Low = f2c ($ weather-> forecast_conditions-> low ['data']);
$ High = f2c ($ weather-> forecast_conditions-> high ['data']);
Return" N {$ City} N {$ Low} N {$ High} N ";
}
Else
{
Return" N N ";
}
}

// Degrees Fahrenheit to degree Celsius
Function f2c ($ fahrenhite)
{
Return floor ($ fahrenhite-32)/1.8 );
}


Client c. php

The code is as follows:



Weather query




If (! Empty ($ _ POST ['city'])
{
$ City = $ _ POST ['city'];
$ Xml = simplexml_load_file ("http: // 127.0.0.1/rest/se. php? City = {$ city }");
$ Html ="

City: {$ xml-> city}

N ";
$ Html. ="

Low: {$ xml-> low}

N ";
$ Html. ="

High: {$ xml-> high}

N ";
Echo $ html;
}
?>




Article address:

Reprint at will ^ please include the address of this article!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.