Php obtains 72-hour weather forecasts for cities [www.weather.com.cn]

Source: Internet
Author: User
Php obtains 72-hour weather forecasts for cities [www.weather.com.cn]
Php obtains 72-hour weather forecasts for cities
Download the weather forecast content data.txt from various cities: http://3.ibtf.sinaapp.com/data.txt?#]
Demo address: http://3.ibtf.sinaapp.com /? City = % E7 % 9F % B3 % E5 % AE % B6 % E5 % BA % 84

  1. Function getweather($city,$data='data.txt ') // Obtain the weather forecast content.
  2. {
  3. $ Urlarr = unserialize (file_get_contents ($ data ));
  4. If ($ urlarr [$ city])
  5. {
  6. $ Url = $ urlarr [$ city]; $ text = $ city;
  7. $ Lines_string = file_get_contents ($ url );
  8. $ Lines_string = explode (" ', '', $ Lines_string [1]), str_replace ('2 -->','', $ lines_string [2]), str_replace ('3 --> ', '', $ lines_string_3 [0]);
  9. For ($ I = 0; $ I <count ($ lines_array); $ I ++)
  10. {
  11. $ Tiqian = array ("℃", "high temperature", "Low temperature"); $ tihou = array ("degree ","",""); $ nowarray = str_replace ($ tiqian, $ tihou, strip_tags ($ lines_array [$ I]);
  12. $ Datearray = explode ("day", $ nowarray); $ wtext [$ I] = trim ($ datearray [0]). "day"; // Get date
  13. $ Weather = explode ("daytime", $ nowarray); $ weather = explode ("nighttime", $ weather [1]);
  14. $ Paiarr = wchangearray (explode ("r", $ weather [0]); // daytime weather
  15. $ Yearr = wchangearray (explode ("r", $ weather [1]); // nighttime weather
  16. If ($ baiarr [0] ==$ yearr [0]) {$ wtext [$ I]. = $ baiarr [0];} else {$ wtext [$ I]. = $ baiarr [0]. "Turn ". $ yearr [0];} // add the weather to the return value
  17. $ Wtext [$ I]. = $ baiarr [1]. "to". $ yearr [1]; // add the temperature to the return value
  18. If ($ baiarr [2] ==$ yearr [2]) {$ wtext [$ I]. = $ baiarr [2];} else {$ wtext [$ I]. = str_replace ("wind", "", $ mongoarr [2]. "Turn ". $ yearr [2]); $ wtext [$ I]. = "wind";} // add the wind direction to the return value
  19. If ($ baiarr [3]! = "Breeze") {$ wtext [$ I]. = $ baiarr [3];} // add the wind to the return value
  20. }
  21. Return $ text. implode ("", $ wtext );
  22. } Else {
  23. Return "unable to obtain the weather information of the city! ";
  24. }
  25. }
  26. Function wchangearray ($ arr) // sorts the values of an array.
  27. {
  28. Foreach ($ arr as $ v)
  29. {If (! Trim ($ v) continue;
  30. $ Value [] = trim ($ v);} return $ value;
  31. }

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.