PHP微信API接口类

(编辑:jimmy 日期: 2024/10/1 浏览:2)

本文实例为大家分享了PHP微信接口类,供大家参考,具体内容如下

<"TOKEN", "weixin");
 
//实例化微信对象
$wechatObj = new wechatCallbackapiTest();
//验证成功后注释valid方法
//$wechatObj->valid();
//开启自动回复功能
$wechatObj->responseMsg();
 
 
//定义类文件
class wechatCallbackapiTest
{
  //实现valid验证方法:实现对接微信公众平台
  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
    //接受用户端发送过来的xml数据
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 
    //extract post data
    //判断xml数据是否为空
    if (!empty($postStr)){
        /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
          the best way is to check the validity of xml by yourself */
        libxml_disable_entity_loader(true);
        //通过simplexml进行xml解析
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        //接受微信的手机端
        $fromUsername = $postObj->FromUserName;
        //微信公众平台
        $toUsername = $postObj->ToUserName;
        //接受用户发送的关键词
        $keyword = trim($postObj->Content);
        //1.接受用户消息类型
        $msgType = $postObj -> MsgType;
        //时间戳
        $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($msgType == 'text'){
          if(!empty( $keyword ))
          {
            /*这是一个实例
              //如果发送文本信息
              $msgType = "text";
              //回复内容
              if($keyword == "李楠"){
                $contentStr = "叫我干嘛";
              }else{
                $contentStr = "叫我干嘛";
              }
              //格式化xml模板,参数与上面的模板是一一对应的.fromUsername和头Username是相反的,只写带%s的
              $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 
              //将xml信息返回给客户端
              echo $resultStr;
            */
            if($keyword == "" || $keyword == "?"){
              $msgType = "text";
              $contentStr = "1.特种服务号码\n2.通讯服务号码";
              $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
              echo $resultStr;
            }elseif($keyword == 1){
              $msgType = "text";
              $contentStr = "1.匪警:110\n2.火警:119\n3.急救:120";
              $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
              echo $resultStr;
            }elseif($keyword == 2){
              $msgType = "text";
              $contentStr = "1.中国移动:10086\n2.中国联通:10010";
              $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
              echo $resultStr;
            }
          }else{
            echo "不能不说话";
          }
        }
        ////////////////////////////////////////////////////////////////////////////////////
        //接受图片信息
        if($msgType == "image"){
            //如果发送文本信息
            $msgType = "text";
            //回复内容
            $contentStr = "你发送的是图片文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //将xml信息返回给客户端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "voice"){
            //如果发送文本信息
            $msgType = "text";
            //回复内容
            $contentStr = "你发送的是语音文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //将xml信息返回给客户端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "video"){
            //如果发送文本信息
            $msgType = "text";
            //回复内容
            $contentStr = "你发送的是视频文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //将xml信息返回给客户端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "shortvideo"){
            //如果发送文本信息
            $msgType = "text";
            //回复内容
            $contentStr = "你发送的是小视频文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //将xml信息返回给客户端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "location"){
            //如果发送文本信息
            $msgType = "text";
            //回复内容
            $contentStr = "你发送的是地理位置文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //将xml信息返回给客户端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "link"){
            //如果发送文本信息
            $msgType = "text";
            //回复内容
            $contentStr = "你发送的是连接文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //将xml信息返回给客户端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        /*
        //判断用户发送关键词是否为空      
         
        if(!empty( $keyword ))
        {
          //如果发送文本信息
          $msgType = "text";
          //回复内容
          $contentStr = "大家好,我是hero";
          //格式化字符串
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
          //将xml信息返回给客户端
          echo $resultStr;
        }else{
          echo "Input something...";
        }
        */
    }else {
      echo "";
      exit;
    }
  }
     
  private function checkSignature()
  {
    // you must define TOKEN by yourself
    //判断是否定义了TOKEN,如果没有就抛出一个异常
    if (!defined("TOKEN")) {
      throw new Exception('TOKEN is not defined!');
    }
     
    $signature = $_GET["signature"];//接受微信加密签名
    $timestamp = $_GET["timestamp"];//接受时间戳
    $nonce = $_GET["nonce"];//接受随机数
         
    $token = TOKEN;//把TOKEN常量赋值给$token
    //把相关参数组装成数组
    $tmpArr = array($token, $timestamp, $nonce);
    // use SORT_STRING rule
    //排序
    sort($tmpArr, SORT_STRING);
    //把排序后的数组转换成字符串
    $tmpStr = implode( $tmpArr );
    //通过哈希算法加密
    $tmpStr = sha1( $tmpStr );
    //与加密签名进行对比
    if( $tmpStr == $signature ){
      //相同返回true
      return true;
    }else{
      //不同返回false
      return false;
    }
  }
}
 
?>

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

一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?