首頁 > 後端開發 > php教程 > 微信公眾平台天氣查詢,火車票查詢,快遞查詢,小黃雞,翻譯,公車查詢的實現

微信公眾平台天氣查詢,火車票查詢,快遞查詢,小黃雞,翻譯,公車查詢的實現

WBOY
發布: 2016-07-25 08:50:35
原創
1311 人瀏覽過
複製程式碼
大家可以去出處掃描二維碼試試
  1. /**
  2. * 微信php測驗
  3. */
  4. include("ABClient.php"); //愛幫網申請然後下載的sdk
  5. define("FANAPK", "API key"); //這個百度應用程式duapp.com建立一個應用程式獲得的API key
  6. define("WAPK","key"); //這個key位址http:/ /lbsyun.baidu.com/apiconsole/key?application=key來取得
  7. //define your token
  8. define("TOKEN", "這裡寫你自己的token");
  9. $wechatObj = new wechatCallbackapiTest();
  10. $wechatObj->responseMsg();
  11. class wechatCallbackapiTest
  12. {
  13. private $.
  14. $this->model = new ABClient();
  15. $a = $this->model->test_transfer();
  16. $this->name = include("name.php");
  17. }
  18. public function valid()
  19. {
  20. $echoStr = $_GET["echostr"];
  21. //valid signature , option
  22. if($this-1 checkSignature()){
  23. echo $echoStr;
  24. exit;
  25. }
  26. }
  27. public function responseMsg()
  28. {
  29. public function responseMsg()
  30. {
  31. > data, May be due to the different environments
  32. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  33. //extract post data
  34. if (!empty($postStr)){
  35. if (!empty($postStr)){
  36. if (!empty($postStr)){
  37. if (!empty($postStr)){
  38. if (!empty($postStr)){
  39. if (!empty($postStr)){
  40. if (!empty($postStr)){
  41. if (!empty($postStr)){
  42. if (!empty($postStr)){
  43. if (!empty($postStr)){
  44. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  45. $fromUsername = $postObj->FromUserName;
  46. $toUsername = $postObj-Vis; ->MsgType;
  47. $event = $postObj->Event;
  48. $c = $postObj->Location_X;
  49. $d = $postObj->Location_Y;
  50. $keyword = trim($postObjj ->Content);
  51. $time = time();
  52. $msgid = $postObj->MsgId;
  53. $textTpl = "
  54. %s
  55. 0
  56. ";
  57. if($type == "event" && $event == "subscribe")
  58. {
  59. $msgType = "text";
  60. $contentStr.= "歡迎關注微度網絡,該公眾平台有以下幾種功能:nn";
  61. $contentStr.= "1.直接輸入漢字或中文進行中英文翻譯,如“你好”n";
  62. $contentStr.= "2.輸入“天氣區」進行天氣查詢,如「天氣石家莊」n";
  63. $contentStr.= "3.輸入「城市起點終點」進行公車查詢,如「石家莊火車站宮家莊」n";
  64. $contentStr.= "4.輸入「@任何內容」跟小賤雞聊天,如「@小賤雞」n";
  65. $contentStr.= "5.微信發送您的地理位置進行天氣查詢,您可以試試n";
  66. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  67. echo $resultStr;
  68. }else if ($type == "text"){
  69. $keywords = explode(" ",$keyword);
  70. $b = explode("@",$keyword); if(isset($b [1])){ $contentStr = $this->simsim($b[1]); $msgType = "text"; $resultStr = sprintf($textTpl, $fromUsername, $ toUsername, $time, $msgType, $contentStr); echo $resultStr; exit(); }
  71. if($keywords[0]=="火車"){
  72. $city = $keywords[1];
  73. $end = $keywords[2];
  74. $time = $keywords[ 3];
  75. $result = $this->doget($city,$end,$time);
  76. if(!empty($result)){
  77. $result = strip_tags($result[' datas']);
  78. $return_str = str_replace(" ","",$result);
  79. $return_str = str_replace("\n","",$return_str);
  80. $a = explode (",",$return_str);
  81. $name =array();
  82. $c = array_chunk($a,16);
  83. array_pop($c);
  84. foreach($c as $k =>$v){
  85. $str="餘票:n商務座:".$v[5].",特等座:".$v[6].",一等座:" .$v[7].",二等座:".$v[8].",高級軟臥:".$v[9].",軟臥:".$v[10].",硬臥: ".$v[11].",軟座:".$v[12].",硬座:".$v[13].",無座:".$v[14].",其他: ".$v[15];
  86. $str = preg_replace("/硬座:--,/","",$str);
  87. $str = preg_replace("/商務座:--,/ ","",$str);
  88. $str = preg_replace("/特等座:--,/","",$str);
  89. $str = preg_replace("/一等座:- -,/","",$str);
  90. $str = preg_replace("/二等座:--,/","",$str);
  91. $str = preg_replace("/高級軟臥:--,/","",$str);
  92. $str = preg_replace("/軟臥:--,/","",$str);
  93. $str = preg_replace("/硬臥:--,/","",$str);
  94. $str = preg_replace("/軟座:--,/","",$str);
  95. $str = preg_replace(" /硬座:--,/","",$str);
  96. $str = preg_replace("/其他:--/","",$str);
  97. $contentStr.="車次: {$v[1]},發站:{$v[2]},到站:{$v[3]},歷時:{$v[4]}。 n{$str}nn";
  98. if($k == 12){
  99. break;
  100. }
  101. }
  102. }else{
  103. $contentStr.="抱歉沒檢查到";
  104. }
  105. $msgType = "text";
  106. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  107. echo $resultStr;
  108. exit();
  109. }
  110. if(isset($keywords[1])&&!isset($keywords[2])){
  111. if($this->pexpress (trim($keywords[0]))){
  112. $a = $this->express(trim($keywords[0]),trim($keywords[1]));
  113. if($a ["message"]=="ok"){
  114. if($a['ischeck']==1){
  115. $contentStr.="您的包裹已簽收nn";
  116. }else {
  117. $contentStr.="您的包裹還未簽收nn";
  118. }
  119. $data = $a['data'];
  120. foreach($data as $k =>$v ){
  121. $contentStr.="更新時間:{$v["time"]}n物流狀態:{$v["context"]}nn";
  122. }
  123. }else{
  124. $contentStr = "查詢失敗,請輸入正確後查詢";
  125. }
  126. $msgType = "text";
  127. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $ msgType, $contentStr);
  128. echo $resultStr;
  129. }else{
  130. $weather = $this->weather($keywords[1]);
  131. if(!empty($weather))
  132. {
  133. $data = $this->json2array($w​​eather);
  134. foreach($data as $k =>$v)
  135. {
  136. $str.=$v[' date'].'。天氣:'.$v['weather'].'。風速:'.$v['wind'].'。溫度:'.$v['temperature']."nn";
  137. }
  138. }else{
  139. $str="請您輸入正確的地址,輸入方式如天氣北京";
  140. }
  141. $msgType = "text";
  142. $contentStr = $str;
  143. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  144. echo $resultStr;
  145. }
  146. }else if(isset($keywords[2])&&($keywords[0]!=="火車")){
  147. $city = $keywords[0];
  148. $start = $keywords[1];
  149. $end =$keywords[2];
  150. $a = $this->model->test_transfer($city,$ start,$end);
  151. if(!empty($a)){
  152. foreach($a as $k=>$v){
  153. $contentStr.= "線路{$c}:" .$v['dist']."米。估計耗費時間:{$v['time']}分鐘。n乘車線:{$v['segments']["segment"][0][" line_name"]}。n上車地點:{$v['segments']["segment"][0]["start_stat"]}。n下車地點:{$v['segments']["segment" ][0]["end_stat"]}。n經過路線:{$v['segments']["segment"][0]["stats"]}nn";
  154. if($k == 4){
  155. break;
  156. }
  157. }
  158. }else{
  159. $contentStr="抱歉沒有查詢到";
  160. }
  161. $msgType = "text";
  162. $resultStr = sprintf ($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  163. echo $resultStr;
  164. }else{
  165. $fanyi = $this->fanyi($keyword)
  166. $msgType = "text";
  167. $contentStr = $fanyi;
  168. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  169. echo $ resultStr ;
  170. }
  171. }else if($type == '位置'){
  172. $result = $this->address($c,$d);
  173. if (isset ( $result['district'])){
  174. $weather = $this->weather($result['district']);
  175. if(!$weather){
  176. $weather = $this - >天氣($result['city']);
  177. }
  178. }else{
  179. $weather = $this->weather($result['city']);
  180. }
  181. $data = $this->json2array($w​​eather);
  182. foreach($data as $k =>$v){
  183. $str.=$v[ 'date' ] .'。溫度:'.$v['temperature']."nn";
  184. }
  185. $msgType = "text";
  186. $contentStr = $str;
  187. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  188. echo $resultStr;
  189. }else{
  190. echo "您可以輸入點什麼";
  191. }
  192. }else {
  193. echo "";
  194. exit;
  195. }
  196. }
  197. //取得地理位置的
  198. private function address($addx,$addy)
  199. {
  200. $arr = array();
  201. $result = $this->map_text("http://api.map.baidu.com/geocoder/v2/?ak=".WAPK."&callback=renderReverse&location ={$addx},{$addy}&output=xml&pois=0");
  202. $data = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
  203. $result = $this->json2array($data );
  204. $arr = $result["result"]["addressComponent"];
  205. $address = $result['result']['formatted_address'];
  206. array_push($arr,$address );
  207. return $arr;
  208. }
  209. //解析json為數組
  210. private function json2array($json) {
  211. if ($json) {
  212. foreach ((array) $json as $k=>$v) {
  213. $data[$k] = !is_string($v) ? $this->json2array($v) : $v;
  214. }
  215. return $ data;
  216. }
  217. }
  218. //取得天氣資訊
  219. private function weather($data)
  220. {
  221. //依照API呼叫輸出資料
  222. $url="http ://api.map.baidu.com/telematics/v2/weather?location={$data}&output=json&ak=".WAPK;
  223. $weather=$this->map_text($url);
  224. $data = json_decode($weather);
  225. return $data->results;
  226. }
  227. //呼叫翻譯的api
  228. private function fanyi($data)
  229. {
  230. / /根據API呼叫輸出資料
  231. $url="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".FANAPK."&q={$data}&from=auto&to=auto" ;
  232. $fanyi=$this->map_text($url);
  233. $shuju=json_decode($fanyi);
  234. $result=$shuju->trans_result;
  235. return $result[0] ->dst;
  236. }
  237. //解析位址的api
  238. private function map_text($url)
  239. {
  240. if(!function_exists('file_get_contents')) {
  241. $1_contents = file_get_contents($url);
  242. } else {
  243. $ch = curl_init();
  244. $timeout = 5;
  245. curl_setopt ($ch, CURLOPT_URL, $url);
  246. curl_setopt ($ch, CURLOPT_URL, $url);
  247. $ch, CURLOPT_RETURNTRANSFER, 1);
  248. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  249. $file_contents = curl_exec($ch);
  250. curl_close>; return $file_contents;
  251. }
  252. private function checkSignature()
  253. {
  254. $signature = $_GET["signature"];
  255. $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];
  256. $token = TOKEN;
  257. $tmpArr = array($token, $timestamp, $nonce);
  258. sort($tmpArr);
  259. $tmpStr = implode( $tmpArr );
  260. $tmpStr = sha1( $tmpStr );
  261. if( $tmpStr == $signature ){
  262. return true;
  263. } else{
  264. return false;
  265. }
  266. }
  267. private function simsim($keywords){
  268. $curlPost=array("txt"=>$keywords);
  269. $curlPost=array("txt"=>$keywords);
  270. $ch = curl_init();//初始化curl
  271. curl_setopt($ch,CURLOPT_URL,'http://xiaohuangji.com/ajax.aspx');//抓取指定網頁
  272. curl_setopt($ch, CURLOPT_HEADER, 0 );//設定header
  273. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字串且輸出到螢幕上
  274. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  275. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  276. $data = curl_exec($ch);//運行curl
  277. curl_close($ch);
  278. return $data;
  279. private function pexpress($exname){
  280. $a = $this->map_text("http://www.kuaidi100.com/");
  281. preg_match_all("/data-code="(? Pw )">(?P.*)/iU",$a,$b);
  282. $name = array();
  283. foreach( $b['title'] as $k=>$v){
  284. $name[$v] = $b['name'][$k];
  285. }
  286. if(!empty( $name[$exname])){
  287. return true;
  288. }else{
  289. return false;
  290. }
  291. }
  292. private function express($keywords,$number){
  293. $a = $this->map_text("http://www.kuaidi100.com/");
  294. preg_match_all("/data-code="(?Pw )"> (?P.*)/iU",$a,$b);
  295. $name = array();
  296. foreach($b['title'] as $k= >$v){
  297. $name[$v] = $b['name'][$k];
  298. }
  299. $this->name = $name;
  300. $keywords = $ name[$keywords];
  301. $url = "http://www.kuaidi100.com/query?type={$keywords}&postid={$number}";
  302. $result = $this->map_text ($url);
  303. $result = $this->json2array(json_decode($result));
  304. return $result;
  305. }
  306. private function doget ($start,$end ,$time) // get取得資料使用量
  307. {
  308. if(empty($time)){
  309. $time = date('Y-m-d',time());
  310. }else{
  311. if(substr($time,0,1)!=0){
  312. $time = date('Y-0',time()).$time;
  313. }else{
  314. $time = date('Y-',time()).$time;
  315. }
  316. }
  317. $star = $this->name[$start];
  318. $end = $this->name[$end];
  319. $url = "http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date={$time}&Request.from. $star}&orderRequest.to_station_telecode={$end}&orderRequest.train_no=&trainPassType=QB&trainClass=QB#D#Z#T#K#QT#&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str_0:000 🎜> $optionget = array('http' => array('method' => "GET", 'header' => "User-Agent:Mozilla/4.0 (相容; MSIE 7.0; Windows NT 6.0 ; .NET CLR 2.0 .50727; .NET CLR 3.5.21022; CIBA)rn接受:*/*rnReferer:http://dynamic.12306.cn/otsquery/查詢/queryRemanentTicketAction.do?method=init"));
  320. $file = file_get_contents($url, false , stream_context_create($optionget));
  321. return $this->json2array(json_decode($file));
  322. }
>?>


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板