WeChat development-weather forecast function

Source: Internet
Author: User
This article mainly gives you a detailed introduction to the development of Baidu weather forecast, which has some reference value, interested friends can refer to this article for details about the Baidu weather forecast developed by php, which has some reference value. interested friends can refer to it.

This article provides an example of the php Baidu weather forecast development code for your reference. The details are as follows:

1. apply to log on to Baidu ak:Http://www.php.cn/

 $ Ak, 'location' => $ location, 'output' => $ output); $ querystring = http_build_query ($ querystring_arrays ); // Generate sn $ sn = md5 (urlencode ($ uri. '? '. $ Querystring. $ sk); $ targetUrl = sprintf ($ url, $ ak, urlencode ($ location), $ output, $ sn); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ targetUrl); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); $ result = curl_exec ($ ch); curl_close ($ ch ); $ result = json_decode ($ result, true); if ($ result ["error"]! = 0) {return $ result ["status"] ;}$ curHour = (int) date ('H', time ()); $ weather = $ result ["results"] [0]; $ weatherArray [] = array ("Title" => $ weather ['currentcity']. "Weather Forecast", "Description" => "", "PicUrl" => "", "Url" => ""); for ($ I = 0; $ I
 
  
$ Weather ["weather_data"] [$ I] ["data"]. "\ n ". $ weather ["weather_data"] [$ I] ["weather"]. $ weather ["weather_data"] [$ I] ["wind"]. $ weather ["weather_data"] [$ I] ["temperature"], "Description" => "", "PicUrl" => ($ curHour> = 6) & ($ curHour <18 ))? $ Weather ["weather_data"] [$ I] ["dayPictureUrl"]: $ weather ["weather_data"] [$ I] ["nightPictureUrl"], "URL" => "");} return $ weatherArray ;}
 

3. implement weather message events


 ResponseMsg ();} else {$ wechatObj-> valid ();} class wechatCallbackapiTest {// verify the signature public function valid () {$ echoStr = $ _ GET ["echostr"]; if ($ this-> checkSignature () {header ('content-type: text'); echo $ echoStr; exit ;}} public function checkSignature () {$ signature =$ _ GET ["signature"]; $ timestamp =$ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ non Ce); sort ($ tmpArr); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr = $ signature) {return true;} else {return false;} // response message public function responseMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Empty ($ postStr) {$ this-> logger ("R ". $ postStr); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ RX_TYPE = trim ($ postObj-> MsgType ); // switch ($ RX_TYPE) {case "event": $ result = $ this-> receiveEvent ($ postObj); break; case "text ": $ result = $ this-> deleetext ($ postObj); break; default: $ result = "unknown msg type :". $ RX_TYPE; break;} echo $ result;} else {echo ""; Exit ;}// public function receiveEvent ($ object) {$ content = ""; switch ($ object-> Event) {case "subscribe ": $ content = "Welcome to Nicky's public account"; $ content. = (! Empty ($ object-> EventKey ))? ("\ N from the QR code scenario ". str_replace ("qrscene _", "", $ object-> EventKey): ""; break; case "unsubscribe": $ content = "cancel follow"; break ;} $ result = $ this-> transmitText ($ object, $ content); return $ result;} // receives the text message public function paietext ($ object) {$ keyword = trim ($ object-> Content); // if (strstr ($ keyword, "weather") {$ city = str_replace ('weather ', '', $ keyword); include (" weather. php "); $ content = getWeatherInfo ($ cit Y) ;}$ result = $ this-> transmitNews ($ object, $ content); return $ result;} // reply to the public function transmitNews ($ object, $ newsArray) {if (! Is_array ($ newsArray) {return;} $ itemTpl ="
    
  <! [CDATA [% s]>   
  %s
     
  %s
     
  %s
    
 "; $ Item_str =" "; foreach ($ newsArray as $ item) {$ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['URL']);} $ xmlTpl ="
  
  %s
   
  %s
   
  
   
% S
   
  news
  % S$ Item_str 
 "; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ newsArray); return $ result ;} // public function logger ($ log_content) {if (isset ($ _ SERVER ['http _ appname']) {// SAE sae_set_display_errors (false ); sae_debug ($ log_content); sae_set_display_errors (true);} else if ($ _ SERVER ['remote _ ADDR ']! = "127.0.0.1") {// LOCAL $ max_size = 10000; $ log_filename = "log. xml "; if (file_exists ($ log_filename) and (abs (filesize ($ log_filename) >$ max_size) {unlink ($ log_filename);} file_put_contents ($ log_filename, date ('H: I: s '). "". $ log_content. "\ r \ n", FILE_APPEND) ;}}?>

The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.

The above is the development-detailed content of the weather forecast function. For more information, see other related articles on the php Chinese network!

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.