Now the site will have some service stuff. such as calendar, weather forecast and so on! Since Google has permanently closed the weather API, I've also found a weather API that calls Yahoo and Sina.
Yahoo Weather forecast Address http://developer.yahoo.com/weather/
Nonsense not much to say the direct code sticker on:
0) {return true; }}//get Woeid $woeid = Self::getwoeid (); Self:: $url = self:: $url. $woeid [0]; Get day Weather $XML = Self::vget (self:: $url); Save day weather to document 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 $weekindex this week = $index [' Yweather:forecast ']; $week = Array (); foreach ($weekindex as $k = + $v) {$week [$k] = $vals [$v] [' attributes ']; } unset ($index); Unset ($vals); Print_r ($week); /*
* Day Week * Date * Low lowest temperature * High Maximum temperature * te St Weather Status * Code weather icon */}/* * Get Region Woeid code */ST atic 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]->w Oeid; Save to file $woeidSTR = Json_encode ($woeid); File_put_contents (self:: $file _path. Self:: $woeid _file, $woeidSTR); return $rs; }else{//If the city is not found Woeid the default city is Beijing self:: $city = "Beijing"; return Self::getwoeid (); }/** * Create an XML cache * @param $contents the 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, $con Tents) or Die (' no write permission ');} /** * Simulate get content function * @param type $url * @return type */static private functio n 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 access curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists from the certificate curl_setopt ($curl, curlopt_useragent, $user _agent); Simulates the browser @curl_setopt used by the user ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_httpget, 1); Send a regular POST request curl_setopt ($curl, Curlopt_timeout, 120); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl); Execute action if (Curl_errno ($curl)) {Curl_cloSE ($curl); Turn off Curl session die (' Errno '. Curl_error ($curl)); } curl_close ($curl); Turn off the curl session return $tmpInfo; Returns the 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 an XML cache * @param $contents the content to be cached */static private function Cachexml ($contents) {$contents = Str_ireplace ('
', "
\ n ", $contents); File_put_contents (self:: $weatherXML, $contents) or Die (' no write permission ');} /** * Simulate get content function * @param type $url * @return type */static private functio n 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 access curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists from the certificate curl_setopt ($curl, curlopt_useragent, $user _agent); Simulates the browser @curl_setopt used by the user ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_httpget, 1); Send a regular POST request curl_setopt ($curl, Curlopt_timeout, 120); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl); Execute action if (Curl_errno ($curl)) {Curl_close ($CURL); Turn off Curl session die (' Errno '. Curl_error ($curl)); } curl_close ($curl); Turn off the curl session return $tmpInfo; Returns data}}weather::getxml ();
http://www.bkjia.com/PHPjc/440125.html www.bkjia.com true http://www.bkjia.com/PHPjc/440125.html techarticle now the site will have some service stuff. such as calendar, weather forecast and so on! Because Google has permanently closed the weather API so I found another call Yahoo and Sina weather pre ...