首页 >后端开发 >php教程 >微信公众平台天气查询,火车票查询,快递查询,小黄鸡,翻译,公交查询的实现

微信公众平台天气查询,火车票查询,快递查询,小黄鸡,翻译,公交查询的实现

WBOY
WBOY原创
2016-07-25 08:50:351303浏览
大家可以去出处扫描二维码试试
  1. /**
  2. * wechat php test
  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 $model;
  14. private $name;
  15. public function __construct(){
  16. $this->model = new ABClient();
  17. $a = $this->model->test_transfer();
  18. $this->name = include("name.php");
  19. }
  20. public function valid()
  21. {
  22. $echoStr = $_GET["echostr"];
  23. //valid signature , option
  24. if($this->checkSignature()){
  25. echo $echoStr;
  26. exit;
  27. }
  28. }
  29. public function responseMsg()
  30. {
  31. //get post data, May be due to the different environments
  32. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  33. //extract post data
  34. if (!empty($postStr)){
  35. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  36. $fromUsername = $postObj->FromUserName;
  37. $toUsername = $postObj->ToUserName;
  38. $type = $postObj->MsgType;
  39. $event = $postObj->Event;
  40. $c = $postObj->Location_X;
  41. $d = $postObj->Location_Y;
  42. $keyword = trim($postObj->Content);
  43. $time = time();
  44. $msgid = $postObj->MsgId;
  45. $textTpl = "
  46. %s
  47. 0
  48. ";
  49. if($type == "event" && $event == "subscribe")
  50. {
  51. $msgType = "text";
  52. $contentStr.= "欢迎关注微度网络,该公众平台有以下几种功能:\n\n";
  53. $contentStr.= "1.直接输入汉字或者中文进行中英文翻译,如“你好”\n";
  54. $contentStr.= "2.输入“天气+地区”进行天气查询,如“天气+石家庄”\n";
  55. $contentStr.= "3.输入“城市+起点+终点”进行公交查询,如“石家庄+火车站+宫家庄”\n";
  56. $contentStr.= "4.输入“@任何内容”跟小贱鸡聊天,如“@小贱鸡”\n";
  57. $contentStr.= "5.微信发送您的地理位置进行天气查询,您可以试一试\n";
  58. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  59. echo $resultStr;
  60. }else if($type == "text"){
  61. $keywords = explode("+",$keyword);
  62. $b = explode("@",$keyword);
  63. if(isset($b[1])){
  64. $contentStr = $this->simsim($b[1]);
  65. $msgType = "text";
  66. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  67. echo $resultStr;
  68. exit();
  69. }
  70. if($keywords[0]=="火车"){
  71. $city = $keywords[1];
  72. $end = $keywords[2];
  73. $time = $keywords[3];
  74. $result = $this->doget($city,$end,$time);
  75. if(!empty($result)){
  76. $result = strip_tags($result['datas']);
  77. $return_str = str_replace(" ","",$result);
  78. $return_str = str_replace("\\n","",$return_str);
  79. $a = explode(",",$return_str);
  80. $name =array();
  81. $c = array_chunk($a,16);
  82. array_pop($c);
  83. foreach($c as $k =>$v){
  84. $str="余票:\n商务座:".$v[5].",特等座:".$v[6].",一等座:".$v[7].",二等座:".$v[8].",高级软卧:".$v[9].",软卧:".$v[10].",硬卧:".$v[11].",软座:".$v[12].",硬座:".$v[13].",无座:".$v[14].",其他:".$v[15];
  85. $str = preg_replace("/硬座\:--,/","",$str);
  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. $contentStr.="车次:{$v[1]},发站:{$v[2]},到站:{$v[3]},历时:{$v[4]}。\n{$str}\n\n";
  97. if($k == 12){
  98. break;
  99. }
  100. }
  101. }else{
  102. $contentStr.="抱歉没有查到";
  103. }
  104. $msgType = "text";
  105. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  106. echo $resultStr;
  107. exit();
  108. }
  109. if(isset($keywords[1])&&!isset($keywords[2])){
  110. if($this->pexpress(trim($keywords[0]))){
  111. $a = $this->express(trim($keywords[0]),trim($keywords[1]));
  112. if($a["message"]=="ok"){
  113. if($a['ischeck']==1){
  114. $contentStr.="您的包裹已经签收\n\n";
  115. }else{
  116. $contentStr.="您的包裹还未签收\n\n";
  117. }
  118. $data = $a['data'];
  119. foreach($data as $k =>$v){
  120. $contentStr.="更新时间:{$v["time"]}\n物流状态:{$v["context"]}\n\n";
  121. }
  122. }else{
  123. $contentStr = "查询失败,请输入正确后查询";
  124. }
  125. $msgType = "text";
  126. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  127. echo $resultStr;
  128. }else{
  129. $weather = $this->weather($keywords[1]);
  130. if(!empty($weather))
  131. {
  132. $data = $this->json2array($weather);
  133. foreach($data as $k =>$v)
  134. {
  135. $str.=$v['date'].'。天气:'.$v['weather'].'。风速:'.$v['wind'].'。温度:'.$v['temperature']."\n\n";
  136. }
  137. }else{
  138. $str="请您输入正确的地址,输入方式如天气+北京";
  139. }
  140. $msgType = "text";
  141. $contentStr = $str;
  142. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  143. echo $resultStr;
  144. }
  145. }else if(isset($keywords[2])&&($keywords[0]!=="火车")){
  146. $city = $keywords[0];
  147. $start = $keywords[1];
  148. $end =$keywords[2];
  149. $a = $this->model->test_transfer($city,$start,$end);
  150. if(!empty($a)){
  151. foreach($a as $k=>$v){
  152. $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"]}\n\n";
  153. if($k == 4){
  154. break;
  155. }
  156. }
  157. }else{
  158. $contentStr="抱歉没有查询到";
  159. }
  160. $msgType = "text";
  161. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  162. echo $resultStr;
  163. }else{
  164. $fanyi = $this->fanyi($keyword);
  165. $msgType = "text";
  166. $contentStr = $fanyi;
  167. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  168. echo $resultStr;
  169. }
  170. }else if($type == 'location'){
  171. $result = $this->address($c,$d);
  172. if(isset($result['district'])){
  173. $weather = $this->weather($result['district']);
  174. if(!$weather){
  175. $weather = $this->weather($result['city']);
  176. }
  177. }else{
  178. $weather = $this->weather($result['city']);
  179. }
  180. $data = $this->json2array($weather);
  181. foreach($data as $k =>$v){
  182. $str.=$v['date'].'。天气:'.$v['weather'].'。风速:'.$v['wind'].'。温度:'.$v['temperature']."\n\n";
  183. }
  184. $msgType = "text";
  185. $contentStr = $str;
  186. $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  187. echo $resultStr;
  188. }else{
  189. echo "您可以输入点什么";
  190. }
  191. }else {
  192. echo "";
  193. exit;
  194. }
  195. }
  196. //获取地理位置的
  197. private function address($addx,$addy)
  198. {
  199. $arr = array();
  200. $result = $this->map_text("http://api.map.baidu.com/geocoder/v2/?ak=".WAPK."&callback=renderReverse&location={$addx},{$addy}&output=xml&pois=0");
  201. $data = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
  202. $result = $this->json2array($data);
  203. $arr = $result["result"]["addressComponent"];
  204. $address = $result['result']['formatted_address'];
  205. array_push($arr,$address);
  206. return $arr;
  207. }
  208. //解析json为数组
  209. private function json2array($json) {
  210. if ($json) {
  211. foreach ((array)$json as $k=>$v) {
  212. $data[$k] = !is_string($v) ? $this->json2array($v) : $v;
  213. }
  214. return $data;
  215. }
  216. }
  217. //获取天气信息
  218. private function weather($data)
  219. {
  220. //根据API调用输出数据
  221. $url="http://api.map.baidu.com/telematics/v2/weather?location={$data}&output=json&ak=".WAPK;
  222. $weather=$this->map_text($url);
  223. $data = json_decode($weather);
  224. return $data->results;
  225. }
  226. //调用翻译的api
  227. private function fanyi($data)
  228. {
  229. //根据API调用输出数据
  230. $url="http://openapi.baidu.com/public/2.0/bmt/translate?client_id=".FANAPK."&q={$data}&from=auto&to=auto";
  231. $fanyi=$this->map_text($url);
  232. $shuju=json_decode($fanyi);
  233. $result=$shuju->trans_result;
  234. return $result[0]->dst;
  235. }
  236. //解析地址的api
  237. private function map_text($url)
  238. {
  239. if(!function_exists('file_get_contents')) {
  240. $file_contents = file_get_contents($url);
  241. } else {
  242. $ch = curl_init();
  243. $timeout = 5;
  244. curl_setopt ($ch, CURLOPT_URL, $url);
  245. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  246. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  247. $file_contents = curl_exec($ch);
  248. curl_close($ch);
  249. }
  250. return $file_contents;
  251. }
  252. private function checkSignature()
  253. {
  254. $signature = $_GET["signature"];
  255. $timestamp = $_GET["timestamp"];
  256. $nonce = $_GET["nonce"];
  257. $token = TOKEN;
  258. $tmpArr = array($token, $timestamp, $nonce);
  259. sort($tmpArr);
  260. $tmpStr = implode( $tmpArr );
  261. $tmpStr = sha1( $tmpStr );
  262. if( $tmpStr == $signature ){
  263. return true;
  264. }else{
  265. return false;
  266. }
  267. }
  268. private function simsim($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. }
  280. private function pexpress($exname){
  281. $a = $this->map_text("http://www.kuaidi100.com/");
  282. preg_match_all("/data\-code\=\"(?P\w+)\"\>\(?P.*)\/iU",$a,$b);<li> $name = array();</li> <li> foreach($b['title'] as $k=>$v){</li> <li> $name[$v] = $b['name'][$k];</li> <li> }</li> <li> if(!empty($name[$exname])){</li> <li> return true;</li> <li> }else{</li> <li> return false;</li> <li> }</li> <li> }</li> <li> private function express($keywords,$number){</li> <li> $a = $this->map_text("http://www.kuaidi100.com/");</li> <li> preg_match_all("/data\-code\=\"(?P<name>\w+)\"\>\<span>(?P<title>.*)\/iU",$a,$b);<li> $name = array();</li> <li> foreach($b['title'] as $k=>$v){</li> <li> $name[$v] = $b['name'][$k];</li> <li> }</li> <li> $this->name = $name;</li> <li> $keywords = $name[$keywords];</li> <li> $url = "http://www.kuaidi100.com/query?type={$keywords}&postid={$number}";</li> <li> $result = $this->map_text($url);</li> <li> $result = $this->json2array(json_decode($result));</li> <li> return $result;</li> <li> }</li> <li> </li> <li> private function doget ($start,$end,$time) // get获取数据使用</li> <li> {</li> <li> if(empty($time)){</li> <li> $time = date('Y-m-d',time());</li> <li> }else{</li> <li> if(substr($time,0,1)!=0){</li> <li> $time = date('Y-0',time()).$time;</li> <li> }else{</li> <li> $time = date('Y-',time()).$time;</li> <li> }</li> <li> }</li> <li> $star = $this->name[$start];</li> <li> $end = $this->name[$end];</li> <li> $url = "http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date={$time}&orderRequest.from_station_telecode={$star}&orderRequest.to_station_telecode={$end}&orderRequest.train_no=&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00";</li> <li> $optionget = array('http' => array('method' => "GET", 'header' => "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.0.04506; CIBA)\r\nAccept:*/*\r\nReferer:http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=init")); </li> <li> $file = file_get_contents($url, false , stream_context_create($optionget));</li> <li> return $this->json2array(json_decode($file));</li> <li> }</li> <li> </li> <li>}</li> <li> <li>?></li>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn