- Header ('content-Type: text/html; charset = utf-8 ');
- Class weather {
- Static $ url = 'http: // xml.weather.yahoo.com/forecastrss? U = c & w = ';
- Static $ city = 'Beijing'; // The default city is Beijing. Note that the city must be entered in Pinyin. I have tried Chinese and cannot call it in several regions.
- Static $ weatherXML = '';
- Static $ woeid_file = "woeid ";
- Static $ file_path = "data /";
-
- /**
- * Obtain remote xml and cache it locally
- */
- Static public function getXML ($ city = null ){
-
- If ($ city! = Null ){
- Self: $ city = $ 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;
- }
- }
- // Obtain the woeid
- $ Woeid = self: getWOEID ();
- Self: $ url = self: $ url. $ woeid [0];
- // Obtain the weather of the current day
- $ XML = self: vget (self: $ url );
- // Save the day's weather 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 );
-
- // This week's weather
- $ 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
- */
- }
- /*
- * Obtain the region WOEID.
- */
- 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 % 20 woeid % 20 from % 20geo. places % 20 where % 20 text = '". 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 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 by default.
- Self: $ city = "Beijing ";
-
- Return self: getWOEID ();
- }
- }
- /**
- * Create an xml cache
- * @ Param $ contents content to be cached
- */
- 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 ');
- }
- /**
- * Simulated content retrieval functions
- * @ 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); // address to be accessed
- Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
- Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
- Curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // simulate the browser used by the user
- @ Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
- Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
- Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular Post request
- Curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // sets 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 is returned as a file stream.
- $ TmpInfo = curl_exec ($ curl); // perform the operation
- If (curl_errno ($ curl )){
- Curl_close ($ curl); // Close the CURL session
- Die ('errno'. curl_error ($ curl ));
- }
- Curl_close ($ curl); // Close the CURL session
- Return $ tmpInfo; // return data
- }
- }
- Weather: getXML ("Changsha ");
2, Sina Weather address: http://php.weather.sina.com.cn code:
- Header ('content-Type: text/html; charset = utf-8 ');
-
- Class weather {
- Static $ url = 'http: // php.weather.sina.com.cn/xml.php? Password = DJOYnieT8234jlsK & day = 0 & city = '; // The password is a fixed value.
- Static $ city = '% B1 % B1 % BE % A9'; // Default city: Beijing
- Static $ weatherXML = '';
- Static $ file_path = "data /";
- /**
- * Obtain remote xml and cache it locally
- */
- Static public function getXML ($ city = null ){
-
- If ($ city! = Null ){
- $ 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 an xml cache
- * @ Param $ contents content to be cached
- */
- Static private function cacheXML ($ contents ){
- $ Contents = str_ireplace (' '," \ N ", $ contents );
- File_put_contents (self: $ weatherXML, $ contents) or die ('No write authorization ');
- }
- /**
- * Simulated content retrieval functions
- * @ 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); // address to be accessed
- Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
- Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
- Curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // simulate the browser used by the user
- @ Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
- Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
- Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular Post request
- Curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // sets 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 is returned as a file stream.
- $ TmpInfo = curl_exec ($ curl); // perform the operation
- If (curl_errno ($ curl )){
- Curl_close ($ curl); // Close the CURL session
- Die ('errno'. curl_error ($ curl ));
- }
- Curl_close ($ curl); // Close the CURL session
- Return $ tmpInfo; // return data
- }
- }
- Weather: getXML ();
|