You can try it by scanning the QR code at the source.
- /**
- * wechat php test
- */
- include("ABClient.php"); //Apply and download the sdk from Aibang.com
- define("FNAPK", "API key"); //This Baidu application duapp.com creates an API key obtained by the application
- define("WAPK","key"); //This key address is http://lbsyun.baidu.com/apiconsole/key?application=key 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 = "
- < ToUserName>
-
- %s CreateTime>
-
-
-
- ";
- if($type == "event" && $event == "subscribe")
- {
- $msgType = "text";
- $contentStr.= "Welcome Follow Weidu Network. This public platform has the following functions: nn";
- $contentStr.= "1. Directly input Chinese characters or Chinese characters for Chinese and English translation, such as "Hello"n";
- $contentStr.= "2 .Enter "weather + region" for weather query, such as "Weather + Shijiazhuang"n";
- $contentStr.= "3. Enter "city + starting point + destination" for public transportation query, such as "Shijiazhuang + Railway Station + Gongjiazhuang "n";
- $contentStr.= "4. Enter "@anycontent" to chat with the little bitch, such as "@little bitch"n";
- $contentStr.= "5. Send your geographical location to WeChat for weather information 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 tickets: n business seat:".$v [5].", Special class seat: ".$v[6].", First class seat: ".$v[7].", Second class seat: ".$v[8].", Premium 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("/Premium Class 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("/Others:--/","",$str);
- $contentStr.="Train number: {$v[1]}, departure station: {$v[2]}, arrival station: {$v[3]}, duration: {$v[4]}. n{$str}nn";
- 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 not been signed for nn";
- }else{
- $contentStr.="Your package has not been signed for nn";
- }
- $data = $a['data'];
- foreach($data as $k =>$v){
- $contentStr.="Update time: {$v["time"]}nLogistics status: {$v["context"]}nn";
- }
- }else{
- $contentStr = "Query failed, please enter 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'].'. Wind speed: '.$v['wind'].'. Temperature: '.$v['temperature']."nn";
- }
- }else{
- $str="Please 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($a)){
- foreach($a as $k=>$ v){
- $contentStr.= "Route{$c}:".$v['dist']." meters. Estimated time: {$v['time']} minutes. nBus route: {$ v['segments']["segment"][0]["line_name"]}. n boarding location: {$v['segments']["segment"][0]["start_stat"]}. Drop off location: {$v['segments']["segment"][0]["end_stat"]}.n经过路线:{$v['segments']["segment"][0]["stats"]}nn";
- if($k == 4){
- break;
- }
- }
-
- }else{
- $contentStr="抱歉没有查询到";
- }
-
- $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'].'。天气:'.$v['weather'].'。风速:'.$v['wind'].'。温度:'.$v['temperature']."nn";
- }
- $msgType = "text";
- $contentStr = $str;
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- echo $resultStr;
- }else{
- echo "您可以输入点什么";
- }
-
- }else {
- echo "";
- exit;
- }
- }
- //获取地理位置的
- 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;
- }
- //解析json为数组
- private function json2array($json) {
- if ($json) {
- foreach ((array)$json as $k=>$v) {
- $data[$k] = !is_string($v) ? $this->json2array($v) : $v;
- }
- return $data;
- }
- }
- //获取天气信息
- private function weather($data)
- {
- //根据API调用输出数据
- $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;
- }
- //调用翻译的api
- private function fanyi($data)
- {
- //根据API调用输出数据
- $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
- 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();//初始化curl
- curl_setopt($ch,CURLOPT_URL,'http://xiaohuangji.com/ajax.aspx');//抓取指定网页
- curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return $data;
- }
- private function pexpress($exname){
- $a = $this->map_text("http://www.kuaidi100.com/");
- preg_match_all("/data-code="(?Pw+)">(?P.*)/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="(?Pw+)">(?P.*)/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获取数据使用
- {
- 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)rnAccept:*/*rnReferer: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));
- }
-
- }
-
- ?>
复制代码
|