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

Source: Internet
Author: User
Public platform weather query, train ticket query, express delivery query, little yellow chicken, translation, bus query implementation
You can scan the QR code at the source.

  1. /**
  2. * Wechat php test
  3. */
  4. Include ("ABClient. php"); // The sdk for applying for and downloading
  5. Define ("FANAPK", "API key"); // This Baidu app duapp.com creates the API key obtained by an application
  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 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. %s
  48. % S
  49. %s
  50. %s
  51. 0
  52. ";
  53. If ($ type = "event" & $ event = "subscribe ")
  54. {
  55. $ MsgType = "text ";
  56. $ ContentStr. = "Welcome to pay attention to the micro-level network. this public platform has the following features: \ n ";
  57. $ ContentStr. = "1. directly enter Chinese or Chinese characters for Chinese and English translation, for example," Hello "\ n ";
  58. $ ContentStr. = "2. enter" Weather + region "for weather query, for example," Weather + Shijiazhuang "\ n ";
  59. $ ContentStr. = "3. enter" City + Start Point + end point "for public transit query, for example," Shijiazhuang + railway station + Gongjiazhuang "\ n ";
  60. $ ContentStr. = "4. enter" @ any content "to chat with the chicken, for example," @ 小 \ "\ n ";
  61. $ ContentStr. = "5. send your geographic location for weather query. 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]. ", special seat :". $ v [6]. ", first-class seat :". $ v [7]. ", second-class seat :". $ v [8]. ", advanced soft sleeper :". $ v [9]. ", soft sleeper :". $ v [10]. ", hard sleeper :". $ v [11]. ", soft seat :". $ v [12]. ", hard seat :". $ v [13]. ", no seat :". $ v [14]. ", others :". $ v [15];
  89. $ Str = preg_replace ("/hard seat \: --,/", "", $ str );
  90. $ Str = preg_replace ("/Business seat \: --,/", "", $ str );
  91. $ Str = preg_replace ("/special seat \: --,/", "", $ str );
  92. $ Str = preg_replace ("/first-class seat \: --,/", "", $ str );
  93. $ Str = preg_replace ("/second-class 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. = "train: {$ v [1]}, departure site: {$ v [2]}, arrival site: {$ v [3]}, duration: {$ v [4]}. \ N {$ str} \ n ";
  101. If ($ k = 12 ){
  102. Break;
  103. }
  104. }
  105. } Else {
  106. $ ContentStr. = "Sorry, I have not found it ";
  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 accepted \ n ";
  119. } Else {
  120. $ ContentStr. = "your package has not been accepted \ n ";
  121. }
  122. $ Data = $ a ['data'];
  123. Foreach ($ data as $ k => $ v ){
  124. $ ContentStr. = "Updated at: {$ v [" time "]} \ n logistics status: {$ v [" context "]} \ n ";
  125. }
  126. } Else {
  127. $ ContentStr = "query failed. please enter the correct information before querying ";
  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 ['weate'].'. Wind speed: '. $ v ['wind'].'. Temperature: '. $ v ['temperature']. "\ n ";
  140. }
  141. } Else {
  142. $ Str = "enter the correct address, 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 ($ )){
  155. Foreach ($ a as $ k => $ v ){
  156. $ ContentStr. = "line {$ c}:". $ v ['dist']. "meters. Estimated time consumption: {$ v ['Time']} minutes. \ N route: {$ v ['segments '] ["segment"] [0] ["line_name"]}. \ N boarding location: {$ v ['segments '] ["segment"] [0] ["start_stat"]}. \ N get-off location: {$ v ['segments '] ["segment"] [0] ["end_stat"]}. \ N route: {$ v ['segments '] ["segment"] [0] ["stats"]} \ n ";
  157. If ($ k = 4 ){
  158. Break;
  159. }
  160. }
  161. } Else {
  162. $ ContentStr = "Sorry not found ";
  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 ['weate'].'. Wind 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 "what can you enter ";
  194. }
  195. } Else {
  196. Echo "";
  197. Exit;
  198. }
  199. }
  200. // Obtain the location
  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 as an 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. // Obtain 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. // Call the translation 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. // Address resolution api
  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'); // capture the specified webpage
  276. Curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set the header
  277. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is 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 % 23 T % 23 K % 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; server load balancer ;.. net clr 2.0.50727; Media Center PC 5.0 ;. net clr 3.5.21022 ;. net clr 3.0.04506; CBA) \ 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. ?>

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.