Php calls yahoosinaapi to develop weather forecast _ PHP Tutorial

Source: Internet
Author: User
Tags filetime
Php calls yahoosinaapi to develop weather forecasts. The current website will have some service stuff. Such as calendar and weather forecast! Since google permanently closed the Weather api, I found another website that calls weather Pre-events of yahoo and Sina, which will have some services. Such as calendar and weather forecast! Since google permanently closed the Weather api, I found another weather forecast api called yahoo and Sina.

Yahoo weather forecast address http://developer.yahoo.com/weather/

Without much nonsense, paste the code directly:

 0) {return true ;}/// obtain woeid $ woeid = self: getWOEID (); self ::$ url = self ::$ url. $ woeid [0]; // get the weather of the current day $ XML = self: vget (self: $ url); // Save the weather of the current day to the file self :: cacheXML ($ XML); self: analysisXML ($ XML);} static public function analysisXML ($ simple) {$ p = xml_parser_create (); xml_parse_into_struct ($ p, $ simple, $ vals, $ index); xml_parser_free ($ p); // weather this week $ weekindex = $ index ['yweather: FORECAST ']; $ week = array (); foreach ($ weekindex as $ k => $ v) {$ week [$ k] = $ vals [$ v] ['bubuckets'];} unset ($ index ); unset ($ vals); print_r ($ week );/*
 * Day week * date * low minimum temperature * high maximum temperature * test weather status * code weather icon */}/** Get region WOEID code */static private function getWOEID () {static $ woeid = array (); if (isset ($ woeid [self: $ city]) {return $ woeid [self: $ city];} if (file_exists (self: $ file_path. self: $ woeid_file) {$ woeidSTR = file_get_contents (self ::$ file_path. self: $ woeid_file); $ woeid = json_decode ($ woeidSTR, true); if (isset ($ woeid [self: $ city]) {return $ woeid [self:: $ city] ;}}$ geoPlaces =" http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text= '". Self: $ city. "% 20CH '"; $ XML = simplexml_load_file ($ geoPlaces); if (isset ($ XML-> results-> place [0]) {$ rs = $ woeid [self: $ city] = $ XML-> results-> place [0]-> woeid; // save to the file $ woeidSTR = json_encode ($ woeid); file_put_contents (self ::$ file_path. self: $ woeid_file, $ woeidSTR); return $ rs;} else {// if the woeid city cannot be found, change it to Beijing self: $ city = "Beijing "; return self: getWOEID () ;}/ *** create xml cache * @ param $ content to be cached by contents */static private function cacheXML ($ contents) {$ contents = str_ireplace ('
 ',"
 \ N ", $ contents); $ contents = mb_convert_encoding ($ contents, 'utf-8', 'gbk'); file_put_contents (self ::$ weatherXML, $ contents) or die ('No write authorization');}/*** simulate the function for obtaining content * @ param type $ url * @ return type */static private function vget ($ url ){
$ User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2 ;. net clr 1.1.4322) "; $ curl = curl_init (); // Start a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url ); // curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1 ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // simulate the user's browser @ curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1 ); // use automatic jump curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically set Referer curl_setopt ($ curl, CURLOPT_HTTPGET, 1 ); // send a regular Post request curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // Set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, 0 ); // display the returned Header content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information returns $ tmpInfo = curl_exec ($ curl) as a file stream ); // execute the operation if (curl_errno ($ curl) {curl_close ($ curl); // Close the CURL session die ('errno '. curl_error ($ curl);} curl_close ($ curl); // close CURL session return $ tmpInfo; // return data} weather: getXML ("Changsha ");

Sina http://php.weather.sina.com.cn

 $ City = mb_convert_encoding ($ city, 'gbk', 'utf-8'); self: $ city = urlencode ($ city);} self: $ weatherXML = self:: $ file_path.md5 (self: $ city ). '-weather. XML'; if (file_exists (self: $ weatherXML) {$ fileTime = filemtime (self: $ weatherXML); $ stater = time () -$ fileTime-60*60*2; if ($ stater> 0) {return true ;}$ contents = self: vget (self :$ url. self: $ city); self: cacheXML ($ contents); self: analysisXML ();}/*** parse xml */static public function analysisXML () {$ XML = simplexml_load_file (self: $ weatherXML); print_r ($ XML );} /*** create xml cache ** @ param $ content to be cached by contents */static private function cacheXML ($ contents) {$ contents = str_ireplace ('
 ',"
 \ N ", $ contents); file_put_contents (self ::$ weatherXML, $ contents) or die ('No write authorization ');} /*** simulate the function for obtaining content * @ param type $ url * @ return type */static private function vget ($ url ){
$ User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2 ;. net clr 1.1.4322) "; $ curl = curl_init (); // Start a CURL session curl_setopt ($ curl, CURLOPT_URL, $ url ); // curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1 ); // check from the certificate whether the SSL encryption algorithm has curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // simulate the user's browser @ curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1 ); // use automatic jump curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically set Referer curl_setopt ($ curl, CURLOPT_HTTPGET, 1 ); // send a regular Post request curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // Set the timeout limit to prevent endless loops curl_setopt ($ curl, CURLOPT_HEADER, 0 ); // display the returned Header content curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information returns $ tmpInfo = curl_exec ($ curl) as a file stream ); // execute the operation if (curl_errno ($ curl) {curl_close ($ curl); // Close the CURL session die ('errno '. curl_error ($ curl);} curl_close ($ curl); // close CURL session return $ tmpInfo; // return data} weather: getXML ();

Bytes. Such as calendar and weather forecast! Since google permanently closed the Weather api, I found a weather pre-called yahoo and sina...

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.