自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

微信公眾平臺(tái)接口開發(fā):(4)天氣預(yù)報(bào)

移動(dòng)開發(fā)
本系列教程是微信公眾平臺(tái)開發(fā)的高級(jí)篇,以微信公眾平臺(tái)應(yīng)用天氣神(WeatherGod)為例,講解微信接口開發(fā)過程。本文為第四篇,天氣預(yù)報(bào)功能的實(shí)現(xiàn)。

一、請(qǐng)求數(shù)據(jù)

首先需要能有取得天氣數(shù)據(jù)的接口,這樣的接口網(wǎng)上有很多。比如google, yahoo,天氣網(wǎng)都提供天氣接口

方倍工作室的API已經(jīng)能提供全國(guó)各地的天氣預(yù)報(bào),使用方倍的API無需再建立城市對(duì)應(yīng)關(guān)系表

使用方式為直接在URL中提交城市名稱即可,如果找不到城市名稱,請(qǐng)先做urlencode

調(diào)用url方法:

  1. http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=上海 
  2. //或者做一次urlencode后再提交 
  3. http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=%E6%B7%B1%E5%9C%B3 

返回格式如下:(返回中自帶換行\(zhòng)n操作,不用自己再添加)

  1.     "errcode""0"
  2.     "msgtype""text"
  3.     "text": { 
  4.         "content""【湘潭】天氣實(shí)況 溫度:12℃ 濕度:59% 風(fēng)速:西北風(fēng)3級(jí)\n03月10日 周日 10℃~21℃ 陰轉(zhuǎn)多云 北風(fēng)轉(zhuǎn)南風(fēng)小于3級(jí)\n03月11日 周一 13℃~28℃ 多云 南風(fēng)小于3級(jí)\n03月12日 周二 10℃~22℃ 小雨轉(zhuǎn)陰 北風(fēng)小于3級(jí)\n技術(shù)支持 方倍工作室" 
  5.     } 

一個(gè)完整的請(qǐng)求類似如下:

  1. $url = "http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=%E6%B7%B1%E5%9C%B3"
  2. $weatherJson = file_get_contents($url); 
  3. $weather = json_decode($weatherJson, true); 
  4. $contentStr = $weather['text']['content']; 

如果使用城市名+天氣方式查詢,則先進(jìn)行城市名提?。?/p>

  1. else if (substr($keyword, -6, strlen($keyword)) == "天氣"){ 
  2.     $cityname = trim(substr($keyword, 0, strlen($keyword) - 6)); 

二,效果演示

使用城市名稱查詢天氣預(yù)報(bào):

原文鏈接。本文為方倍工作室原創(chuàng),51CTO授權(quán)轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)聯(lián)系xuchuan(at)51cto.com

完整源代碼見第二頁。

#p#

二、完整源代碼

  1. <?php 
  2. /* 
  3. 【版權(quán)聲明】 
  4.     本軟件產(chǎn)品的版權(quán)歸方倍工作室所有,受《中華人民共和國(guó)計(jì)算機(jī)軟件保護(hù)條例》等知識(shí)產(chǎn)權(quán)法律及國(guó)際條約與慣例的保護(hù)。您獲得的只是本軟件的使用權(quán)。 
  5.  
  6.     您不得: 
  7.     * 在未得到授權(quán)的情況下刪除、修改本軟件及其他副本上一切關(guān)于版權(quán)的信息; 
  8.     * 銷售、出租此軟件產(chǎn)品的任何部分; 
  9.     * 從事其他侵害本軟件版權(quán)的行為。 
  10.  
  11.     如果您未遵守本條款的任一約定,方倍工作室有權(quán)立即終止本條款的執(zhí)行,且您必須立即終止使用本軟件并銷毀本軟件產(chǎn)品的任何副本。這項(xiàng)要求對(duì)各種拷貝形式有效。 
  12.  
  13.     您同意承擔(dān)使用本軟件產(chǎn)品的風(fēng)險(xiǎn),在適用法律允許的最大范圍內(nèi),方倍工作室在任何情況下不就因使用或不能使用本軟件產(chǎn)品所發(fā)生的特殊的、意外的、非直接或間接的損失承擔(dān)賠償責(zé)任。即使已事先被告知該損害發(fā)生的可能性。 
  14.  
  15.     如使用本軟件所添加的任何信息,發(fā)生版權(quán)糾紛,方倍工作室不承擔(dān)任何責(zé)任。 
  16.  
  17.     方倍工作室對(duì)本條款擁有最終解釋權(quán)。 
  18.  
  19.     CopyRight 2013  www.doucube.com  All Rights Reserved 
  20.  
  21. */ 
  22.  
  23. date_default_timezone_set('Asia/Hong_Kong');  //set time zone 
  24. define("TOKEN""http://www.doucube.com"); 
  25.  
  26. $wechatObj = new wechatCallbackapiTest(); 
  27. $wechatObj->responseMsg(); 
  28.  
  29. class wechatCallbackapiTest 
  30.     public function responseMsg() 
  31.     { 
  32.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  33.         logger("R ".$postStr); 
  34.         //extract post data 
  35.         if (!emptyempty($postStr)){ 
  36.             $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA); 
  37.             $RX_TYPE = trim($postObj->MsgType); 
  38.  
  39.             switch ($RX_TYPE
  40.             { 
  41.                 case "text"
  42.                     $resultStr = $this->receiveText($postObj); 
  43.                     break
  44.                 case "event"
  45.                     $resultStr = $this->receiveEvent($postObj); 
  46.                     break
  47.             } 
  48.             logger("T ".$resultStr); 
  49.             echo $resultStr
  50.         }else { 
  51.             echo ""
  52.             exit
  53.         } 
  54.     } 
  55.  
  56.     private function receiveText($object
  57.     { 
  58.         $funcFlag = 0; 
  59.         $keyword = trim($object->Content); 
  60.         $resultStr = ""
  61.         $contentStr = ""
  62.          
  63.         if (substr($keyword, -6, strlen($keyword)) == "天氣"){ 
  64.             $keyword = trim(substr($keyword, 0, strlen($keyword) - 6)); 
  65.             if ($keyword == ""){$keyword = "北京";} 
  66.             $apihost = "http://api2.sinaapp.com/"
  67.             $apimethod = "search/weather/?"
  68.             $apiparams = array('appkey'=>"0020120430"'appsecert'=>"fa6095e113cd28fd"'reqtype'=>"text"); 
  69.             $apikeyword = "&keyword=".urlencode($keyword); 
  70.             $apicallurl = $apihost.$apimethod.http_build_query($apiparams).$apikeyword
  71.             $weatherJson = file_get_contents($apicallurl); 
  72.             $weather = json_decode($weatherJson, true); 
  73.             $contentStr = $weather['text']['content']; 
  74.             $resultStr = $this->transmitText($object$contentStr$funcFlag); 
  75.         }else
  76.             $contentStr = "發(fā)送城市加天氣的命令查詢天氣,如“北京天氣”,“上海天氣”。"
  77.             $resultStr = $this->transmitText($object$contentStr$funcFlag); 
  78.         } 
  79.         return $resultStr
  80.     } 
  81.  
  82.     private function receiveEvent($object
  83.     { 
  84.         $contentStr = ""
  85.         switch ($object->Event) 
  86.         { 
  87.             case "subscribe"
  88.                 $contentStr = "Power By 方倍工作室!"
  89.                 break
  90.         } 
  91.         $resultStr = $this->transmitText($object$contentStr); 
  92.         return $resultStr
  93.     } 
  94.      
  95.     private function transmitText($object$content$flag = 0) 
  96.     { 
  97.         $textTpl = "<xml> 
  98. <ToUserName><![CDATA[%s]]></ToUserName> 
  99. <FromUserName><![CDATA[%s]]></FromUserName> 
  100. <CreateTime>%s</CreateTime> 
  101. <MsgType><![CDATA[text]]></MsgType> 
  102. <Content><![CDATA[%s]]></Content> 
  103. <FuncFlag>%d</FuncFlag> 
  104. </xml>"; 
  105.         $resultStr = sprintf($textTpl$object->FromUserName, $object->ToUserName, time(), $content$flag); 
  106.         return $resultStr
  107.     } 
  108.  
  109.  
  110. function logger($log_content
  111.     if (isset($_SERVER['HTTP_APPNAME'])){ //SAE 
  112.         sae_set_display_errors(false); 
  113.         sae_debug($log_content); 
  114.         sae_set_display_errors(true); 
  115.     }else { 
  116.         $max_size = 100000; 
  117.         $log_filename = date("Ymd").".xml"
  118.         if(file_exists($log_filenameand (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} 
  119.         file_put_contents($log_filenamedate('H:i:s')." ".$log_content."\r\n", FILE_APPEND); 
  120.     } 
  121. ?> 

 

責(zé)任編輯:徐川 來源: 方倍工作室
相關(guān)推薦

2016-03-14 10:29:38

天氣預(yù)報(bào)各類工具源碼

2013-04-10 16:15:40

微信公眾平臺(tái)接口開發(fā)

2013-03-26 13:20:27

Android天氣預(yù)報(bào)

2013-04-10 18:45:52

微信公眾平臺(tái)接口開發(fā)

2009-07-07 09:25:08

Linux開發(fā)FOSS開發(fā)項(xiàng)目

2013-04-10 18:19:40

微信公眾平臺(tái)接口開發(fā)

2013-04-10 18:07:08

微信公眾平臺(tái)接口開發(fā)

2021-02-07 09:17:24

鴻蒙HarmonyOS應(yīng)用開發(fā)

2014-11-20 09:38:40

C#

2017-08-01 10:10:32

人工智能智能天氣預(yù)報(bào)

2010-08-13 10:56:58

FlexWebservice

2013-04-10 18:24:48

微信公眾平臺(tái)接口開發(fā)

2013-04-10 18:29:09

微信公眾平臺(tái)接口開發(fā)

2013-04-15 16:56:48

微信公眾平臺(tái)Android開發(fā)

2013-04-10 16:51:56

微信公眾平臺(tái)接口開發(fā)

2013-09-09 10:52:10

2012-06-18 15:40:32

jQuery

2018-01-29 11:25:37

LinuxASCII 字符天氣預(yù)報(bào)

2020-02-11 20:00:29

開源開源工具天氣預(yù)報(bào)

2013-11-13 07:19:19

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)