欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php微信公众号开发之快递查询

程序员文章站 2022-11-14 21:11:50
本文实例为大家分享了php微信公众号开发之快递查询的具体代码,供大家参考,具体内容如下 快递查询 数组用法 foreach 查询接口是:爱快...

本文实例为大家分享了php微信公众号开发之快递查询的具体代码,供大家参考,具体内容如下

快递查询

  • 数组用法
  • foreach

查询接口是:爱快递:https://www.aikuaidi.cn/api/

核心代码如下:

$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
        $fromusername = $postobj->fromusername;
        $tousername = $postobj->tousername;
        $type = $postobj->msgtype;
        $customrevent = $postobj->event;
        $latitude = $postobj->location_x;
        $longitude = $postobj->location_y;
        $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>";       
        switch ($type)
        { 
        case "text";
          $status=array('0'=>'查询出错','1'=>'暂无记录','2'=>'在途中','3'=>'派送中','4'=>'已签收','5'=>'拒收','6'=>'疑难件','7'=>'退回');//构建快递状态数组
          $kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$keyword}&id=shentong&show=xml";//快递地址
          $kuaidistr=file_get_contents($kuaidiurl);//读入文件
          $kuaidiobj=simplexml_load_string($kuaidistr);//xml解析
          $kuaidistatus = $kuaidiobj->status;//获取快递状态
          $kuaistr=strval($kuaidistatus);//对象转换为字符串
          $contentstr0 =$status[$kuaistr];//根据数组返回
          foreach ($kuaidiobj->data->order as $a)
          {  
            foreach ($a->time as $b)
            {
              foreach ($a->content as $c)
              {
                $m.="{$b}{$c}";}
              }
          }
          //遍历获取快递时间和事件
          $contentstr="你的快递单号{$keyword}{$contentstr0}{$m}";
          break;         
        default;
          $contentstr ="此项功能尚未开发";  
        }
        $msgtype="text";
        $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
        echo $resultstr;

index.php整体代码如下:

<?php
/**
 * wechat php test
 */

//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
//$wechatobj->valid();
$wechatobj->responsemsg();
class wechatcallbackapitest
{
  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;
        $customrevent = $postobj->event;
        $latitude = $postobj->location_x;
        $longitude = $postobj->location_y;
        $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>";       
        switch ($type)
        { 
        case "text";
          $status=array('0'=>'查询出错','1'=>'暂无记录','2'=>'在途中','3'=>'派送中','4'=>'已签收','5'=>'拒收','6'=>'疑难件','7'=>'退回');//构建快递状态数组
          $kuaidiurl="http://www.aikuaidi.cn/rest/?key=ff4735a30a7a4e5a8637146fd0e7cec9&order={$keyword}&id=shentong&show=xml";//快递地址
          $kuaidistr=file_get_contents($kuaidiurl);//读入文件
          $kuaidiobj=simplexml_load_string($kuaidistr);//xml解析
          $kuaidistatus = $kuaidiobj->status;//获取快递状态
          $kuaistr=strval($kuaidistatus);//对象转换为字符串
          $contentstr0 =$status[$kuaistr];//根据数组返回
          foreach ($kuaidiobj->data->order as $a)
          {  
            foreach ($a->time as $b)
            {
              foreach ($a->content as $c)
              {
                $m.="{$b}{$c}";}
              }
          }
          //遍历获取快递时间和事件
          $contentstr="你的快递单号{$keyword}{$contentstr0}{$m}";
          break;         
        default;
          $contentstr ="此项功能尚未开发";  
        }
        $msgtype="text";
        $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
        echo $resultstr;

    }else {
      echo "";
      exit;
    }
  }

  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;
    }
  }
}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。