You can scan the QR code at the source.
- /**
- * Wechat php test
- */
- Include ("ABClient. php"); // The sdk for applying for and downloading
- Define ("FANAPK", "API key"); // This Baidu app duapp.com creates the API key obtained by an application
- Define ("WAPK", "key"); // This key address http://lbsyun.baidu.com/apiconsole/key? Application = key to get
- // Define your token
- Define ("TOKEN", "write your own token here ");
- $ WechatObj = new wechatCallbackapiTest ();
- $ WechatObj-> responseMsg ();
- Class wechatCallbackapiTest
- {
- Private $ model;
- Private $ name;
- Public function _ construct (){
- $ This-> model = new ABClient ();
- $ A = $ this-> model-> test_transfer ();
- $ This-> name = include ("name. php ");
- }
- Public function valid ()
- {
- $ EchoStr = $ _ GET ["echostr"];
- // Valid signature, option
- If ($ this-> checkSignature ()){
- Echo $ echoStr;
- Exit;
- }
- }
- Public function responseMsg ()
- {
- // Get post data, May be due to the different environments
- $ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
- // Extract post data
- If (! Empty ($ postStr )){
-
- $ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
- $ FromUsername = $ postObj-> FromUserName;
- $ ToUsername = $ postObj-> ToUserName;
- $ Type = $ postObj-> MsgType;
- $ Event = $ postObj-> Event;
- $ C = $ postObj-> Location_X;
- $ D = $ postObj-> Location_Y;
- $ Keyword = trim ($ postObj-> Content );
- $ Time = time ();
- $ Msgid = $ postObj-> MsgId;
- $ TextTpl ="
- %s
- %s
- % S
- %s
- %s
- 0
- ";
- If ($ type = "event" & $ event = "subscribe ")
- {
- $ MsgType = "text ";
- $ ContentStr. = "Welcome to pay attention to the micro-level network. this public platform has the following features: \ n ";
- $ ContentStr. = "1. directly enter Chinese or Chinese characters for Chinese and English translation, for example," Hello "\ n ";
- $ ContentStr. = "2. enter" Weather + region "for weather query, for example," Weather + Shijiazhuang "\ n ";
- $ ContentStr. = "3. enter" City + Start Point + end point "for public transit query, for example," Shijiazhuang + railway station + Gongjiazhuang "\ n ";
- $ ContentStr. = "4. enter" @ any content "to chat with the chicken, for example," @ 小 \ "\ n ";
- $ ContentStr. = "5. send your geographic location for weather query. you can try \ n ";
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
-
- } Else if ($ type = "text "){
- $ Keywords = explode ("+", $ keyword );
- $ 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 ();
- }
- If ($ keywords [0] = "train "){
- $ City = $ keywords [1];
- $ End = $ keywords [2];
- $ Time = $ keywords [3];
- $ Result = $ this-> doget ($ city, $ end, $ time );
- If (! Empty ($ result )){
- $ Result = strip_tags ($ result ['datas']);
- $ Return_str = str_replace ("", "", $ result );
- $ Return_str = str_replace ("\ n", "", $ return_str );
- $ A = explode (",", $ return_str );
- $ Name = array ();
- $ C = array_chunk ($ a, 16 );
- Array_pop ($ c );
- Foreach ($ c as $ k => $ v ){
- $ 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];
- $ Str = preg_replace ("/hard seat \: --,/", "", $ str );
- $ Str = preg_replace ("/Business seat \: --,/", "", $ str );
- $ Str = preg_replace ("/special seat \: --,/", "", $ str );
- $ Str = preg_replace ("/first-class seat \: --,/", "", $ str );
- $ Str = preg_replace ("/second-class seat \: --,/", "", $ str );
- $ Str = preg_replace ("/advanced soft sleeper \: --,/", "", $ str );
- $ Str = preg_replace ("/soft sleeper \: --,/", "", $ str );
- $ Str = preg_replace ("/hard sleeper \: --,/", "", $ str );
- $ Str = preg_replace ("/soft seat \: --,/", "", $ str );
- $ Str = preg_replace ("/hard seat \: --,/", "", $ str );
- $ Str = preg_replace ("/other \: --/", "", $ str );
- $ ContentStr. = "train: {$ v [1]}, departure site: {$ v [2]}, arrival site: {$ v [3]}, duration: {$ v [4]}. \ N {$ str} \ n ";
- If ($ k = 12 ){
- Break;
- }
- }
- } Else {
- $ ContentStr. = "Sorry, I have not found it ";
- }
- $ MsgType = "text ";
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
- Exit ();
- }
- If (isset ($ keywords [1]) &! Isset ($ keywords [2]) {
- If ($ this-> pexpress (trim ($ keywords [0]) {
- $ A = $ this-> express (trim ($ keywords [0]), trim ($ keywords [1]);
- If ($ a ["message"] = "OK "){
- If ($ a ['ischeck'] = 1 ){
- $ ContentStr. = "your package has been accepted \ n ";
- } Else {
- $ ContentStr. = "your package has not been accepted \ n ";
- }
- $ Data = $ a ['data'];
- Foreach ($ data as $ k => $ v ){
- $ ContentStr. = "Updated at: {$ v [" time "]} \ n logistics status: {$ v [" context "]} \ n ";
- }
- } Else {
- $ ContentStr = "query failed. please enter the correct information before querying ";
- }
- $ MsgType = "text ";
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
- } Else {
- $ Weather = $ this-> weather ($ keywords [1]);
- If (! Empty ($ weather ))
- {
- $ Data = $ this-> json2array ($ weather );
- Foreach ($ data as $ k => $ v)
- {
- $ Str. = $ v ['Date']. '. Weather: '. $ v ['weate'].'. Wind speed: '. $ v ['wind'].'. Temperature: '. $ v ['temperature']. "\ n ";
- }
- } Else {
- $ Str = "enter the correct address, such as weather + Beijing ";
- }
- $ MsgType = "text ";
- $ ContentStr = $ str;
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
- }
- } Else if (isset ($ keywords [2]) & ($ keywords [0]! = "Train ")){
- $ City = $ keywords [0];
- $ Start = $ keywords [1];
- $ End = $ keywords [2];
- $ A = $ this-> model-> test_transfer ($ city, $ start, $ end );
- If (! Empty ($ )){
- Foreach ($ a as $ k => $ v ){
- $ 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 ";
- If ($ k = 4 ){
- Break;
- }
- }
- } Else {
- $ ContentStr = "Sorry not found ";
- }
- $ MsgType = "text ";
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
- } Else {
- $ Fanyi = $ this-> fanyi ($ keyword );
- $ MsgType = "text ";
- $ ContentStr = $ fanyi;
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
- }
- } Else if ($ type = 'location '){
- $ Result = $ this-> address ($ c, $ d );
- If (isset ($ result ['District ']) {
- $ Weather = $ this-> weather ($ result ['District ']);
- If (! $ Weather ){
- $ Weather = $ this-> weather ($ result ['city']);
- }
- } Else {
- $ Weather = $ this-> weather ($ result ['city']);
- }
- $ Data = $ this-> json2array ($ weather );
- Foreach ($ data as $ k => $ v ){
- $ Str. = $ v ['Date']. '. Weather: '. $ v ['weate'].'. Wind speed: '. $ v ['wind'].'. Temperature: '. $ v ['temperature']. "\ n ";
- }
- $ MsgType = "text ";
- $ ContentStr = $ str;
- $ ResultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ contentStr );
- Echo $ resultStr;
- } Else {
- Echo "what can you enter ";
- }
- } Else {
- Echo "";
- Exit;
- }
- }
- // Obtain the location
- Private function address ($ addx, $ addy)
- {
- $ Arr = array ();
- $ Result = $ this-> map_text ("http://api.map.baidu.com/geocoder/v2? Ak = ". WAPK." & callback = renderReverse & location = {$ addx}, {$ addy} & output = xml & pois = 0 ");
- $ Data = simplexml_load_string ($ result, 'simplexmlelement', LIBXML_NOCDATA );
- $ Result = $ this-> json2array ($ data );
- $ Arr = $ result ["result"] ["addressComponent"];
- $ Address = $ result ['result'] ['formatted _ address'];
- Array_push ($ arr, $ address );
- Return $ arr;
- }
- // Parse json as an array
- Private function json2array ($ json ){
- If ($ json ){
- Foreach (array) $ json as $ k => $ v ){
- $ Data [$ k] =! Is_string ($ v )? $ This-> json2array ($ v): $ v;
- }
- Return $ data;
- }
- }
- // Obtain weather information
- Private function weather ($ data)
- {
- // Output data based on API calls
- $ Url = "http://api.map.baidu.com/telematics/v2/weather? Location = {$ data} & output = json & ak = ". WAPK;
- $ Weather = $ this-> map_text ($ url );
- $ Data = json_decode ($ weather );
- Return $ data-> results;
- }
- // Call the translation api
- Private function fanyi ($ data)
- {
- // Output data based on API calls
- $ Url = "http://openapi.baidu.com/public/2.0/bmt/translate? Client_id = ". FANAPK." & q = {$ data} & from = auto & to = auto ";
- $ Fanyi = $ this-> map_text ($ url );
- $ Shuju = json_decode ($ fanyi );
- $ Result = $ shuju-> trans_result;
- Return $ result [0]-> dst;
- }
- // Address resolution api
- Private function map_text ($ url)
- {
- If (! Function_exists ('File _ get_contents ')){
- $ File_contents = file_get_contents ($ url );
- } Else {
- $ Ch = curl_init ();
- $ Timeout = 5;
- Curl_setopt ($ ch, CURLOPT_URL, $ url );
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
- $ File_contents = curl_exec ($ ch );
- Curl_close ($ ch );
- }
- Return $ file_contents;
- }
- Private function checkSignature ()
- {
- $ Signature = $ _ GET ["signature"];
- $ Timestamp = $ _ GET ["timestamp"];
- $ Nonce = $ _ GET ["nonce"];
-
- $ Token = TOKEN;
- $ TmpArr = array ($ token, $ timestamp, $ nonce );
- Sort ($ tmpArr );
- $ TmpStr = implode ($ tmpArr );
- $ TmpStr = sha1 ($ tmpStr );
- If ($ tmpStr = $ signature ){
- Return true;
- } Else {
- Return false;
- }
- }
- Private function simsim ($ keywords ){
- $ CurlPost = array ("txt" => $ keywords );
- $ Ch = curl_init (); // initialize curl
- Curl_setopt ($ ch, CURLOPT_URL, 'http: // xiaohuangji.com/ajax.aspx'); // capture the specified webpage
- Curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set the header
- Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is a string and output to the screen.
- Curl_setopt ($ ch, CURLOPT_POST, 1); // post submission method
- Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
- $ Data = curl_exec ($ ch); // run curl
- Curl_close ($ ch );
- Return $ data;
- }
- Private function pexpress ($ exname ){
- $ A = $ this-> map_text ("http://www.kuaidi100.com /");
- Preg_match_all ("/data \-code \ = \"(? P \ W +) \ "\> \(? P . *) \ <\/Span>/iU ", $ a, $ B );
- $ Name = array ();
- Foreach ($ B ['title'] as $ k => $ v ){
- $ Name [$ v] = $ B ['name'] [$ k];
- }
- If (! Empty ($ name [$ exname]) {
- Return true;
- } Else {
- Return false;
- }
- }
- Private function express ($ keywords, $ number ){
- $ A = $ this-> map_text ("http://www.kuaidi100.com /");
- Preg_match_all ("/data \-code \ = \"(? P \ W +) \ "\> \(? P . *) \ <\/Span>/iU ", $ a, $ B );
- $ Name = array ();
- Foreach ($ B ['title'] as $ k => $ v ){
- $ Name [$ v] = $ B ['name'] [$ k];
- }
- $ This-> name = $ name;
- $ Keywords = $ name [$ keywords];
- $ Url = "http://www.kuaidi100.com/query? Type = {$ keywords} & postid = {$ number }";
- $ Result = $ this-> map_text ($ url );
- $ Result = $ this-> json2array (json_decode ($ result ));
- Return $ result;
- }
- Private function doget ($ start, $ end, $ time) // get data usage
- {
- If (empty ($ time )){
- $ Time = date ('Y-m-D', time ());
- } Else {
- If (substr ($ time, 0, 1 )! = 0 ){
- $ Time = date ('Y-0', time (). $ time;
- } Else {
- $ Time = date ('Y-', time (). $ time;
- }
- }
- $ Star = $ this-> name [$ start];
- $ End = $ this-> name [$ end];
- $ 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 ";
- $ 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 "));
- $ File = file_get_contents ($ url, false, stream_context_create ($ optionget ));
- Return $ this-> json2array (json_decode ($ file ));
- }
- }
- ?>
|