We can go to the source, scan the QR code, try it.
- /**
- * WeChat PHP Test
- */
- Include ("abclient.php"); Love Help Web application then download the SDK
- Define ("fanapk", "API key"); This Baidu application duapp.com create an app to get the API key
- 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 is 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 the Micro Network, the public platform has the following features: \ n";
- $contentStr. = "1. Direct input of Chinese characters or Chinese translation in English, such as" Hello "\ n";
- $contentStr. = "2. Enter" Weather + area "for weather inquiries, such as" weather + Shijiazhuang "\ n";
- $contentStr. = "3. Enter" City + start + destination "For bus inquiries, such as" Shijiazhuang + railway station + Palace House "\ n";
- $contentStr. = "4. Enter" @ any Content "chat with a small chicken, such as" @ Small chicken "\ n";
- $contentStr. = "5. Send your location for weather inquiries, 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]. ", 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];
- $str = Preg_replace ("/Hard Seat \:--,/", "", $str);
- $str = Preg_replace ("/Business seat \:--,/", "", $str);
- $str = Preg_replace ("/principal seat \:--,/", "", $str);
- $str = Preg_replace ("/Block \:--,/", "", $str);
- $str = Preg_replace ("/second 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. = "n°: {$v [1]}, Outbound: {$v [2]}, Arrival: {$v [3]}, Duration: {$v [4]}. \n{$str}\n\n ";
- if ($k = = 12) {
- Break
- }
- }
- }else{
- $contentStr. = "Sorry not found";
- }
- $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 signed for \ n";
- }else{
- $contentStr. = "Your package has not yet been signed for \ n";
- }
- $data = $a [' data '];
- foreach ($data as $k = = $v) {
- $contentStr. = "Updated: {$v [" Time "]}\n Logistics status: {$v [" context "]}\n\n";
- }
- }else{
- $CONTENTSTR = "Query failed, please enter the correct query";
- }
- $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 [' weather ']. '. Speed: '. $v [' Wind ']. Temperature: '. $v [' temperature ']. " \ n ";
- }
- }else{
- $STR = "Please enter the correct address, enter the way 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 ($a)) {
- foreach ($a as $k = = $v) {
- $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 ";
- if ($k = = 4) {
- Break
- }
- }
- }else{
- $CONTENTSTR = "Sorry not to inquire";
- }
- $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 [' weather ']. '. Speed: '. $v [' Wind ']. Temperature: '. $v [' temperature ']. " \ n ";
- }
- $msgType = "text";
- $CONTENTSTR = $str;
- $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);
- Echo $resultStr;
- }else{
- echo "You can enter something";
- }
- }else {
- echo "";
- Exit
- }
- }
- Get the location of
- 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 array
- Private Function Json2array ($json) {
- if ($json) {
- foreach ((array) $json as $k = + $v) {
- $data [$k] =!is_string ($v)? $this->json2array ($v): $v;
- }
- return $data;
- }
- }
- Get 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;
- }
- Calling the translated 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;
- }
- API to resolve addresses
- 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 ');//crawl specified Web page
- curl_setopt ($ch, Curlopt_header, 0);//Set HEADER
- curl_setopt ($ch, Curlopt_returntransfer, 1);//requires the result to be 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%23t%23k%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; 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 "));
- $file = File_get_contents ($url, False, Stream_context_create ($optionget));
- return $this->json2array (Json_decode ($file));
- }
- }
- ?>
Copy Code |