本文實例講述了微信公眾平台之快遞查詢功能用法。分享給大家供大家參考。具體如下:
使用方法:
#查快遞(或三個首字母ckd)#快遞編號#快遞單號
如(查詢EMS單號為1034616494006的快遞):
#ckd#15#1034616494006
常用快遞編號:<br />
15EMS,56申通,57順豐,77圓通,
80韻達,87中通,85宅急送,62天天
1. class.base.php文件:
<?php /** * 微信公眾平台基礎類 For Fshare * @author: Skiychan * @created: 2013.11.19 */ class Wechat{ // public $token = ''; /* public function __construct($token){ $this->token = $token; } */ //判斷是否來自微信服務器 /* public function valid(){ $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } */ public function responseMsg(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty($keyword)){ //當輸入文字類型 if($msgType = "text"){ //$contentStr = $keyword.$fromUsername.$time; //$preg = "/^\#([\W\w]*?)\#([\W\w]*?)\#(.*)/"; //0為功能,1為選項,2為余下的值 //$preg = "/\#([\w]+)/"; //0為功能,1為選項,2為余下的值 $preg = "/\#([\w\d\x{4e00}-\x{9fa5}]+)/u"; //0為功能,1為選項,2為余下的值 preg_match_all($preg,$keyword,$newdata); $fn_name = $newdata[1][0]; //獲取快遞的參數 if(strtolower($fn_name)=="skiy"){ $me = "我是此公眾帳號的開發者,英文名Skiy。\n如果你對本功能有任何意見和建議,歡迎聯系我,\n我的QQ是1005043848\n微信是forskiy,郵箱:\[email protected]"; $contentStr = $me; } //獲取快遞的參數 if($fn_name=="快遞"||strtolower($fn_name)=="kuaidi"){ $kdlist = ''; //啟用PDO連接sqlite的方式 $db = new PDO("sqlite:somedata/data.dat"); $results = $db->query('SELECT * FROM kuaidi')->fetchAll(); foreach ($results as $key=>$row) { //var_dump($row); //$list_one = $row['id']." ".$row['code']." ".$row['company']."\n"; $list_one = $row['id'].' '.$row['company']."\n"; $kdlist = $kdlist.$list_one; } //主機支持sqlite3的方式 /* $db = new SQLite3("somedata/data.dat"); $results = $db->query('SELECT * FROM kuaidi'); while ($row = $results->fetchArray()) { //var_dump($row); $list_one = $row['id']." ".$row['code']." ".$row['company']."\n\r"; $kdlist = $kdlist.$list_one; } */ $contentStr = $kdlist; } if($fn_name=="查快遞"||strtolower($fn_name)=="ckd"){ $id = $newdata[1][1]; $num = $newdata[1][2]; $db = new PDO("sqlite:somedata/data.dat"); $results = $db->query('SELECT code,company FROM kuaidi WHERE id = '.$id)->fetchAll(); $code = $results[0]['code']; //獲取英文代碼 $com = $results[0]['company']; //獲取公司名稱 $numinfo = "快遞:".$com."\n"."單號:".$num."\n"; $kd_url = "http://m.kuaidi100.com/query?type=".$code."&postid=".$num; $json_getdata = file_get_contents($kd_url); $get_kdinfo = json_decode($json_getdata); //object $get_kdinfo = json_decode($json_getdata,true); //array $last_t = "查詢2. index.php文件:
<?php /** * * 微信公眾平台消息接口 For Fshare(分享資訊) * @author: Skiychan (Wechat:forskiy) * @created:2013.11.19 * */ include_once 'class.base.php'; define('TOKEN', 'skiychan'); $wx = new Wechat(); //$wx->valid(); $wx->responseMsg();希望本文所述對大家的php程序設計有所幫助。