扣丁学堂PHP培训如何进行PHP微信公众号开发之快递查询

2019-05-10 11:13:26 2611浏览

本篇文章扣丁学堂PHP培训小编给读者们分享如何进行PHP微信公众号开发之快递查询,文中有代码列出供小伙伴们参考,对PHP开发技术感兴趣的小伙伴就随小编来了解一下吧,希望对小伙伴有帮助。



扣丁学堂PHP培训如何进行PHP微信公众号开发之快递查询



递查询:


数组用法


foreach



核心代码如下:


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

?>


最后想要了解更多关于PHP方面内容的小伙伴,请关注扣丁学堂PHP培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的PHP开发环境搭建视频,PHP培训后的前景无限,行业薪资和未来的发展会越来越好的,扣丁学堂老师精心推出的PHP视频教程定能让你快速掌握PHP从入门到精通开发实战技能。扣丁学堂PHP技术交流群:374332265。


扣丁学堂微信公众号                          Python全栈开发爬虫人工智能机器学习数据分析免费公开课直播间


【关注微信公众号获取更多学习资料】         【扫码进入Python全栈开发免费公开课】



查看更多关于“php培训资讯”的相关文章>>



标签: PHP培训 PHP基础教程 PHP学习视频 PHP教学视频 PHP入门教程 PHP教程视频 PHP在线学习 PHP在线视频 PHP在线教程 扣丁学堂PHP培训

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号