程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 微信公眾平台之快遞查詢功能用法實例,公眾實例

微信公眾平台之快遞查詢功能用法實例,公眾實例

編輯:關於PHP編程

微信公眾平台之快遞查詢功能用法實例,公眾實例


本文實例講述了微信公眾平台之快遞查詢功能用法。分享給大家供大家參考。具體如下:

使用方法:

#查快遞(或三個首字母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 = "查詢時間:\n".$get_kdinfo['updatetime']."\n\n";  //查詢時間
      $kd_shipinfo = $get_kdinfo['data']; //快遞數據數組
      $kd_total = count($kd_shipinfo)-1;
      $ship = '';
      //物流倒序詳情
      for($i = $kd_total;$i>=0;$i--){
       $shipinfo = $kd_shipinfo[$i]['time']."\n".$kd_shipinfo[$i]['context']."\n";
       $ship = $shipinfo.$ship;
      }
      //順序物流詳情
      /*foreach ($kd_shipinfo as $v){
       $shipinfo = $v['time']."\n".$v['context']."\n";
       $ship = $shipinfo.$ship;
      }
      */
      $get_kdinfo = $numinfo.$last_t."【物流詳情】\n".$ship;
      if($ship){
       $contentStr = $get_kdinfo;
      }else{
       $contentStr = $numinfo.">沒有物流數據!";
      }
     }
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     echo $resultStr;
    }else{
     echo "Input something...";
    }
   }
  }else {
   echo "You have no enter something...";
   exit;
  }
 }
 //判斷簽名,返回bool
 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;
  }
 }
}

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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved