WeChat public platform weather query, train ticket query, express query, small yellow chicken, translation, bus query implementation

Source: Internet
Author: User
Tags cdata sprintf
We can go to the source, scan the QR code, try it.
  1. /**
  2. * WeChat PHP Test
  3. */
  4. Include ("abclient.php"); Love Help Web application then download the SDK
  5. Define ("fanapk", "API key"); This Baidu application duapp.com create an app to get the API key
  6. Define ("wapk", "key"); This key address Http://lbsyun.baidu.com/apiconsole/key?application=key to get
  7. Define your Token
  8. Define ("token", "Write your own token here");
  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 is 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. %s
  48. %s
  49. %s
  50. %s
  51. 0
  52. ";
  53. if ($type = = "Event" && $event = = "Subscribe")
  54. {
  55. $msgType = "text";
  56. $contentStr. = "Welcome to the Micro Network, the public platform has the following features: \ n";
  57. $contentStr. = "1. Direct input of Chinese characters or Chinese translation in English, such as" Hello "\ n";
  58. $contentStr. = "2. Enter" Weather + area "for weather inquiries, such as" weather + Shijiazhuang "\ n";
  59. $contentStr. = "3. Enter" City + start + destination "For bus inquiries, such as" Shijiazhuang + railway station + Palace House "\ n";
  60. $contentStr. = "4. Enter" @ any Content "chat with a small chicken, such as" @ Small chicken "\ n";
  61. $contentStr. = "5. Send your location for weather inquiries, you can try \ n";
  62. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  63. Echo $resultStr;
  64. }else if ($type = = "Text") {
  65. $keywords = Explode ("+", $keyword);
  66. $b = Explode ("@", $keyword);
  67. if (Isset ($b [1])) {
  68. $CONTENTSTR = $this->simsim ($b [1]);
  69. $msgType = "text";
  70. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  71. Echo $resultStr;
  72. Exit ();
  73. }
  74. if ($keywords [0]== "Train") {
  75. $city = $keywords [1];
  76. $end = $keywords [2];
  77. $time = $keywords [3];
  78. $result = $this->doget ($city, $end, $time);
  79. if (!empty ($result)) {
  80. $result = Strip_tags ($result [' datas ']);
  81. $return _str = Str_replace ("", "", $result);
  82. $return _str = str_replace ("\\n", "", $return _str);
  83. $a = explode (",", $return _str);
  84. $name =array ();
  85. $c = Array_chunk ($a, 16);
  86. Array_pop ($c);
  87. foreach ($c as $k = = $v) {
  88. $str = "remaining ticket: \ n Business seat:". $v [5]. ", the principal seat:". $v [6]. ". $v [7].", second-class seat: ". $v [8].", Advanced soft Sleeper: ". $v [9].", soft Sleeper: ". $v [10].", Hard sleeper: Soft: ". $v [12].", Hard seat: ". $v [13].", no Block: ". $v [14].", Other: ". $v [15];
  89. $str = Preg_replace ("/Hard Seat \:--,/", "", $str);
  90. $str = Preg_replace ("/Business seat \:--,/", "", $str);
  91. $str = Preg_replace ("/principal seat \:--,/", "", $str);
  92. $str = Preg_replace ("/Block \:--,/", "", $str);
  93. $str = Preg_replace ("/second seat \:--,/", "", $str);
  94. $str = Preg_replace ("/Advanced soft Sleeper \:--,/", "", $str);
  95. $str = Preg_replace ("/Soft Sleeper \:--,/", "", $str);
  96. $str = Preg_replace ("/Hard Sleeper \:--,/", "", $str);
  97. $str = Preg_replace ("/Soft seat \:--,/", "", $str);
  98. $str = Preg_replace ("/Hard Seat \:--,/", "", $str);
  99. $str = Preg_replace ("/Other \:--/", "" ", $str);
  100. $contentStr. = "n°: {$v [1]}, Outbound: {$v [2]}, Arrival: {$v [3]}, Duration: {$v [4]}. \n{$str}\n\n ";
  101. if ($k = = 12) {
  102. Break
  103. }
  104. }
  105. }else{
  106. $contentStr. = "Sorry not found";
  107. }
  108. $msgType = "text";
  109. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  110. Echo $resultStr;
  111. Exit ();
  112. }
  113. if (Isset ($keywords [1]) &&!isset ($keywords [2])) {
  114. if ($this->pexpress (Trim ($keywords [0])) {
  115. $a = $this->express (Trim ($keywords [0]), Trim ($keywords [1]);
  116. if ($a ["Message"]== "OK") {
  117. if ($a [' Ischeck ']==1) {
  118. $contentStr. = "Your package has been signed for \ n";
  119. }else{
  120. $contentStr. = "Your package has not yet been signed for \ n";
  121. }
  122. $data = $a [' data '];
  123. foreach ($data as $k = = $v) {
  124. $contentStr. = "Updated: {$v [" Time "]}\n Logistics status: {$v [" context "]}\n\n";
  125. }
  126. }else{
  127. $CONTENTSTR = "Query failed, please enter the correct query";
  128. }
  129. $msgType = "text";
  130. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  131. Echo $resultStr;
  132. }else{
  133. $weather = $this->weather ($keywords [1]);
  134. if (!empty ($weather))
  135. {
  136. $data = $this->json2array ($weather);
  137. foreach ($data as $k = $v)
  138. {
  139. $str. = $v [' Date ']. Weather: '. $v [' weather ']. '. Speed: '. $v [' Wind ']. Temperature: '. $v [' temperature ']. " \ n ";
  140. }
  141. }else{
  142. $STR = "Please enter the correct address, enter the way such as weather + Beijing";
  143. }
  144. $msgType = "text";
  145. $CONTENTSTR = $str;
  146. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  147. Echo $resultStr;
  148. }
  149. }else if (Isset ($keywords [2]) && ($keywords [0]!== "Train")} {
  150. $city = $keywords [0];
  151. $start = $keywords [1];
  152. $end = $keywords [2];
  153. $a = $this->model->test_transfer ($city, $start, $end);
  154. if (!empty ($a)) {
  155. foreach ($a as $k = = $v) {
  156. $contentStr. = "line {$c}:" $v [' Dist ']. " Meters. Estimated time-consuming: {$v [' Hour ']} minutes. \ n Bus line: {$v [' segments '] ["segment"][0]["Line_name"]}. \ n Pickup Location: {$v [' segments '] ["segment"][0]["Start_stat"]}. \ n drop-off location: {$v [' segments '] ["segment"][0]["End_stat"]}. \ nthe route: {$v [' segments '] ["segment"][0]["stats"]}\n\n ";
  157. if ($k = = 4) {
  158. Break
  159. }
  160. }
  161. }else{
  162. $CONTENTSTR = "Sorry not to inquire";
  163. }
  164. $msgType = "text";
  165. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  166. Echo $resultStr;
  167. }else{
  168. $fanyi = $this->fanyi ($keyword);
  169. $msgType = "text";
  170. $CONTENTSTR = $fanyi;
  171. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  172. Echo $resultStr;
  173. }
  174. }else if ($type = = ' Location ') {
  175. $result = $this->address ($c, $d);
  176. if (Isset ($result [' District '])) {
  177. $weather = $this->weather ($result [' District ']);
  178. if (! $weather) {
  179. $weather = $this->weather ($result [' City ']);
  180. }
  181. }else{
  182. $weather = $this->weather ($result [' City ']);
  183. }
  184. $data = $this->json2array ($weather);
  185. foreach ($data as $k = = $v) {
  186. $str. = $v [' Date ']. Weather: '. $v [' weather ']. '. Speed: '. $v [' Wind ']. Temperature: '. $v [' temperature ']. " \ n ";
  187. }
  188. $msgType = "text";
  189. $CONTENTSTR = $str;
  190. $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
  191. Echo $resultStr;
  192. }else{
  193. echo "You can enter something";
  194. }
  195. }else {
  196. echo "";
  197. Exit
  198. }
  199. }
  200. Get the location of
  201. Private function Address ($ADDX, $addy)
  202. {
  203. $arr = Array ();
  204. $result = $this->map_text ("http://api.map.baidu.com/geocoder/v2/?ak=". wapk. " &callback=renderreverse&location={$addx},{$addy}&output=xml&pois=0 ");
  205. $data = simplexml_load_string ($result, ' simplexmlelement ', libxml_nocdata);
  206. $result = $this->json2array ($data);
  207. $arr = $result ["Result"] ["addresscomponent"];
  208. $address = $result [' result '] [' formatted_address '];
  209. Array_push ($arr, $address);
  210. return $arr;
  211. }
  212. Parse JSON array
  213. Private Function Json2array ($json) {
  214. if ($json) {
  215. foreach ((array) $json as $k = + $v) {
  216. $data [$k] =!is_string ($v)? $this->json2array ($v): $v;
  217. }
  218. return $data;
  219. }
  220. }
  221. Get Weather information
  222. Private function Weather ($data)
  223. {
  224. Output data based on API calls
  225. $url = "http://api.map.baidu.com/telematics/v2/weather?location={$data}&output=json&ak=". wapk;
  226. $weather = $this->map_text ($url);
  227. $data = Json_decode ($weather);
  228. return $data->results;
  229. }
  230. Calling the translated API
  231. Private Function Fanyi ($data)
  232. {
  233. Output data based on API calls
  234. $url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=". fanapk. " &q={$data}&from=auto&to=auto ";
  235. $fanyi = $this->map_text ($url);
  236. $shuju =json_decode ($fanyi);
  237. $result = $shuju->trans_result;
  238. return $result [0]->dst;
  239. }
  240. API to resolve addresses
  241. Private Function Map_text ($url)
  242. {
  243. if (!function_exists (' file_get_contents ')) {
  244. $file _contents = file_get_contents ($url);
  245. } else {
  246. $ch = Curl_init ();
  247. $timeout = 5;
  248. curl_setopt ($ch, Curlopt_url, $url);
  249. curl_setopt ($ch, Curlopt_returntransfer, 1);
  250. curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
  251. $file _contents = curl_exec ($ch);
  252. Curl_close ($ch);
  253. }
  254. return $file _contents;
  255. }
  256. Private Function Checksignature ()
  257. {
  258. $signature = $_get["signature"];
  259. $timestamp = $_get["timestamp"];
  260. $nonce = $_get["nonce"];
  261. $token = token;
  262. $TMPARR = Array ($token, $timestamp, $nonce);
  263. Sort ($TMPARR);
  264. $TMPSTR = implode ($TMPARR);
  265. $TMPSTR = SHA1 ($TMPSTR);
  266. if ($tmpStr = = $signature) {
  267. return true;
  268. }else{
  269. return false;
  270. }
  271. }
  272. Private Function Simsim ($keywords) {
  273. $curlPost =array ("txt" = $keywords);
  274. $ch = Curl_init ();//Initialize Curl
  275. curl_setopt ($ch, Curlopt_url, ' http://xiaohuangji.com/ajax.aspx ');//crawl specified Web page
  276. curl_setopt ($ch, Curlopt_header, 0);//Set HEADER
  277. curl_setopt ($ch, Curlopt_returntransfer, 1);//requires the result to be a string and output to the screen
  278. curl_setopt ($ch, Curlopt_post, 1);//post Submission method
  279. curl_setopt ($ch, Curlopt_postfields, $curlPost);
  280. $data = curl_exec ($ch);//Run Curl
  281. Curl_close ($ch);
  282. return $data;
  283. }
  284. Private Function Pexpress ($exname) {
  285. $a = $this->map_text ("http://www.kuaidi100.com/");
  286. Preg_match_all ("/data\-code\=\" (? P \w+) \ "\>\ (? P . *) \<\/span>/iu ", $a, $b);
  287. $name = Array ();
  288. foreach ($b [' title '] as $k = + $v) {
  289. $name [$v] = $b [' name '] [$k];
  290. }
  291. if (!empty ($name [$exname])) {
  292. return true;
  293. }else{
  294. return false;
  295. }
  296. }
  297. Private Function Express ($keywords, $number) {
  298. $a = $this->map_text ("http://www.kuaidi100.com/");
  299. Preg_match_all ("/data\-code\=\" (? P \w+) \ "\>\ (? P . *) \<\/span>/iu ", $a, $b);
  300. $name = Array ();
  301. foreach ($b [' title '] as $k = + $v) {
  302. $name [$v] = $b [' name '] [$k];
  303. }
  304. $this->name = $name;
  305. $keywords = $name [$keywords];
  306. $url = "http://www.kuaidi100.com/query?type={$keywords}&postid={$number}";
  307. $result = $this->map_text ($url);
  308. $result = $this->json2array (Json_decode ($result));
  309. return $result;
  310. }
  311. Private Function Doget ($start, $end, $time)//Get Data usage
  312. {
  313. if (empty ($time)) {
  314. $time = Date (' y-m-d ', Time ());
  315. }else{
  316. if (substr ($time, 0, 1)!=0) {
  317. $time = Date (' Y-0 ', Time ()). $time;
  318. }else{
  319. $time = date (' Y ', Time ()). $time;
  320. }
  321. }
  322. $star = $this->name[$start];
  323. $end = $this->name[$end];
  324. $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 ";
  325. $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 "));
  326. $file = File_get_contents ($url, False, Stream_context_create ($optionget));
  327. return $this->json2array (Json_decode ($file));
  328. }
  329. }
  330. ?>
Copy Code
  • Related Article

    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.