大家可以去出處掃描二維碼試試
-
/**
- * 微信php測驗
- */
- include("ABClient.php"); //愛幫網申請然後下載的sdk
- define("FANAPK", "API key"); //這個百度應用程式duapp.com建立一個應用程式獲得的API key
- define("WAPK","key"); //這個key位址http:/ /lbsyun.baidu.com/apiconsole/key?application=key來取得
- //define your token
- define("TOKEN", "這裡寫你自己的token");
- $wechatObj = new wechatCallbackapiTest();
- $wechatObj->responseMsg();
-
- class wechatCallbackapiTest
- {
- private $.
- $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-1 checkSignature()){
- echo $echoStr;
- exit;
- }
- }
-
- public function responseMsg()
- {
- public function responseMsg()
- {
- > data, May be due to the different environments
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
-
- //extract post data
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
- if (!empty($postStr)){
-
- $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- $fromUsername = $postObj->FromUserName;
- $toUsername = $postObj-Vis; ->MsgType;
- $event = $postObj->Event;
- $c = $postObj->Location_X;
- $d = $postObj->Location_Y;
- $keyword = trim($postObjj ->Content);
- $time = time();
- $msgid = $postObj->MsgId;
- $textTpl = "
-
-
- %s
-
-
- 0
-
";
- if($type == "event" && $event == "subscribe")
- {
- $msgType = "text";
- $contentStr.= "歡迎關注微度網絡,該公眾平台有以下幾種功能:nn";
- $contentStr.= "1.直接輸入漢字或中文進行中英文翻譯,如“你好”n";
- $contentStr.= "2.輸入“天氣區」進行天氣查詢,如「天氣石家莊」n";
- $contentStr.= "3.輸入「城市起點終點」進行公車查詢,如「石家莊火車站宮家莊」n";
- $contentStr.= "4.輸入「@任何內容」跟小賤雞聊天,如「@小賤雞」n";
- $contentStr.= "5.微信發送您的地理位置進行天氣查詢,您可以試試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]=="火車"){
- $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="餘票:n商務座:".$v[5].",特等座:".$v[6].",一等座:" .$v[7].",二等座:".$v[8].",高級軟臥:".$v[9].",軟臥:".$v[10].",硬臥: ".$v[11].",軟座:".$v[12].",硬座:".$v[13].",無座:".$v[14].",其他: ".$v[15];
- $str = preg_replace("/硬座:--,/","",$str);
- $str = preg_replace("/商務座:--,/ ","",$str);
- $str = preg_replace("/特等座:--,/","",$str);
- $str = preg_replace("/一等座:- -,/","",$str);
- $str = preg_replace("/二等座:--,/","",$str);
- $str = preg_replace("/高級軟臥:--,/","",$str);
- $str = preg_replace("/軟臥:--,/","",$str);
- $str = preg_replace("/硬臥:--,/","",$str);
- $str = preg_replace("/軟座:--,/","",$str);
- $str = preg_replace(" /硬座:--,/","",$str);
- $str = preg_replace("/其他:--/","",$str);
- $contentStr.="車次: {$v[1]},發站:{$v[2]},到站:{$v[3]},歷時:{$v[4]}。 n{$str}nn";
- if($k == 12){
- break;
- }
- }
- }else{
- $contentStr.="抱歉沒檢查到";
- }
-
- $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.="您的包裹已簽收nn";
- }else {
- $contentStr.="您的包裹還未簽收nn";
- }
- $data = $a['data'];
- foreach($data as $k =>$v ){
- $contentStr.="更新時間:{$v["time"]}n物流狀態:{$v["context"]}nn";
- }
- }else{
- $contentStr = "查詢失敗,請輸入正確後查詢";
- }
- $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'].'。天氣:'.$v['weather'].'。風速:'.$v['wind'].'。溫度:'.$v['temperature']."nn";
- }
- }else{
- $str="請您輸入正確的地址,輸入方式如天氣北京";
- }
-
- $msgType = "text";
- $contentStr = $str;
- $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- echo $resultStr;
- }
-
- }else if(isset($keywords[2])&&($keywords[0]!=="火車")){
- $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.= "線路{$c}:" .$v['dist']."米。估計耗費時間:{$v['time']}分鐘。n乘車線:{$v['segments']["segment"][0][" line_name"]}。n上車地點:{$v['segments']["segment"][0]["start_stat"]}。n下車地點:{$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 == '位置'){
- $result = $this->address($c,$d);
- if (isset ( $result['district'])){
- $weather = $this->weather($result['district']);
- if(!$weather){
- $weather = $this - >天氣($result['city']);
- }
- }else{
- $weather = $this->weather($result['city']);
- }
-
- $data = $this->json2array($weather);
- foreach($data as $k =>$v){
- $str.=$v[ 'date' ] .'。溫度:'.$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')) {
- $1_contents = file_get_contents($url);
- } else {
- $ch = curl_init();
- $timeout = 5;
- curl_setopt ($ch, CURLOPT_URL, $url);
- curl_setopt ($ch, CURLOPT_URL, $url);
- $ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = curl_exec($ch);
- curl_close>; 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);
- $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}&Request.from. $star}&orderRequest.to_station_telecode={$end}&orderRequest.train_no=&trainPassType=QB&trainClass=QB#D#Z#T#K#QT#&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str_0:000 🎜> $optionget = array('http' => array('method' => "GET", 'header' => "User-Agent:Mozilla/4.0 (相容; MSIE 7.0; Windows NT 6.0 ; .NET CLR 2.0 .50727; .NET CLR 3.5.21022; CIBA)rn接受:*/*rnReferer:http://dynamic.12306.cn/otsquery/查詢/queryRemanentTicketAction.do?method=init"));
- $file = file_get_contents($url, false , stream_context_create($optionget));
- return $this->json2array(json_decode($file));
- }
-
-
-
-
-
>?>
|
複製程式碼