Php obtains the current google weather implementation program

Source: Internet
Author: User
We will see that many websites can display the weather in the local area in real time. let me tell you how to do this kind of real-time weather. you can use the googleaip interface to get the weather in different cities and

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:

  1. $ City = $ _ GET ['city'];
  2. $ Data = createXml ($ city );
  3.  
  4. $ Xml = simplexml_load_string ($ data );
  5. Header ("Content-type: text/xml ");
  6. Echo $ xml-> asXML ();
  7.  
  8. // Generate XML data
  9. Function createXml ($ city)
  10. {
  11. // Google Weather API
  12. $ Weather = simplexml_load_file ("http://www.google.com/ig/api? Weather = {$ city }");
  13. If (isset ($ weather-> forecast_conditions ))
  14. {
  15. $ Low = f2c ($ weather-> forecast_conditions-> low ['data']);
  16. $ High = f2c ($ weather-> forecast_conditions-> high ['data']);
  17. Return" N {$ City} N {$ Low} N {$ High} N ";
  18. }
  19. Else
  20. {
  21. Return" N N ";
  22. }
  23. }
  24.  
  25. // Degrees Fahrenheit to degree Celsius
  26. Function f2c ($ fahrenhite)
  27. {
  28. Return floor ($ fahrenhite-32)/1.8 );
  29. }

Client c. php,The code is as follows:

  1.  
  2.  
  3. Weather query 
  4.  
  5.  
  6.  
  7. If (! Emptyempty ($ _ POST ['city'])
  8. {
  9. $ City = $ _ POST ['city'];
  10. $ Xml = simplexml_load_file ("http: // 127.0.0.1/rest/se. php? City = {$ city }");
  11. $ Html ="

    City: {$ xml-> city}

    N ";
  12. $ Html. ="

    Low: {$ xml-> low}

    N ";
  13. $ Html. ="

    High: {$ xml-> high}

    N ";
  14. Echo $ html;
  15. }
  16. ?>
  17.  

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.