Php obtains the current google weather implementation program

Source: Internet
Author: User

Se. php

The code is as follows: Copy code

<? Php
$ 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 "<weather> n <city >{$ city }</city> n <low >{$ low }</low> n     }
Else
    {
Return "<weather> n </weather> n ";
    }
}
 
// Degrees Fahrenheit to degree Celsius
Function f2c ($ fahrenhite)
{
Return floor ($ fahrenhite-32)/1.8 );
}

 

Client c. php

 

The code is as follows: Copy code
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> weather query </title>
</Head>
<Body>
<Form method = "post" action = "">
<Select name = "city">
<Option value = "0"> select </option>
<Option value = "beijing"> beijing </option>
<Option value = "shanghai"> shanghai </option>
<Option value = "guangzhou"> guangzhou </option>
<Option value = "wuhan"> wuhan </option>
</Select>
<Input type = "submit"/>
</Form>
 
<? Php
If (! Empty ($ _ POST ['city'])
{
$ City = $ _ POST ['city'];
$ Xml = simplexml_load_file ("http: // 127.0.0.1/rest/se. php? City = {$ city }");
$ Html = "<p> City: {$ xml-> city} </p> n ";
$ Html. = "<p> Low: {$ xml-> low} </p> n ";
$ Html. = "<p> High: {$ xml-> high} </p> n ";
Echo $ html;
}
?>
</Body>
</Html>

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.