PHP微信API接口类


Posted in PHP onAugust 22, 2016

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

<?php
/**
 * wechat php test
 */
 
//define your token
//定义TOKEN秘钥
define("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;
    }
  }
}
 
?>

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

PHP 相关文章推荐
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
Dec 06 PHP
pdo中使用参数化查询sql
Aug 11 PHP
php curl的深入解析
Jun 02 PHP
MongoDB在PHP中的常用操作小结
Feb 20 PHP
php异常处理使用示例
Feb 25 PHP
php导出生成word的方法
Dec 25 PHP
php求今天、昨天、明天时间戳的简单实现方法
Jul 28 PHP
php分页原理 分页代码 分页类制作教程
Sep 23 PHP
yii2局部关闭(开启)csrf的验证的实例代码
Jul 10 PHP
[原创]PHP实现字节数Byte转换为KB、MB、GB、TB的方法
Aug 31 PHP
php实现的支付宝网页支付功能示例【基于TP5框架】
Sep 16 PHP
PHP实现单条sql执行多个数据的insert语句方法
Oct 11 PHP
简单PHP会话(session)说明介绍
Aug 21 #PHP
PHP中ID设置自增后不连续的原因分析及解决办法
Aug 21 #PHP
PHP控制前台弹出对话框的实现方法
Aug 21 #PHP
php代码检查代理ip的有效性
Aug 19 #PHP
PHP中new static()与new self()的比较
Aug 19 #PHP
ThinkPHP3.2.1图片验证码实现方法
Aug 19 #PHP
PHP自定义图片缩放函数实现等比例不失真缩放的方法
Aug 19 #PHP
You might like
用php简单实现加减乘除计算器
2014/01/06 PHP
ThinkPHP的I方法使用详解
2014/06/18 PHP
thinkPHP线上自动加载异常与修复方法实例分析
2016/12/01 PHP
PHP实现类似于C语言的文件读取及解析功能
2017/09/01 PHP
PHP实现浏览器中直接输出图片的方法示例
2018/03/14 PHP
jQuery 1.5 源码解读 面向中高阶JSER
2011/04/05 Javascript
JS+CSS实现一个气泡提示框
2013/08/18 Javascript
checkbox全选所涉及到的知识点介绍
2013/12/31 Javascript
使用时间戳解决ie缓存的问题
2014/08/20 Javascript
JavaScript对Cookie进行读写操作实例
2015/07/25 Javascript
JavaScript子窗口调用父窗口变量和函数的方法
2015/10/09 Javascript
js实现文本框输入文字个数限制代码
2015/12/25 Javascript
简单谈谈vue的过渡动画(推荐)
2017/10/11 Javascript
vue项目开发中setTimeout等定时器的管理问题
2018/09/13 Javascript
在Web关闭页面时发送Ajax请求的实现方法
2019/03/07 Javascript
Vue实现商品详情页的评价列表功能
2019/09/04 Javascript
JavaScript实现飞舞的泡泡效果
2020/02/07 Javascript
[00:32]DOTA2上海特级锦标赛 COL战队宣传片
2016/03/04 DOTA
[01:11:15]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
[09:59]DOTA2-DPC中国联赛2月7日Recap集锦
2021/03/11 DOTA
Python中关于Sequence切片的下标问题详解
2017/06/15 Python
Python文件的读写和异常代码示例
2017/10/31 Python
Python实现聊天机器人的示例代码
2018/07/09 Python
对Python之gzip文件读写的方法详解
2019/02/08 Python
python射线法判断一个点在图形区域内外
2019/06/28 Python
pytorch SENet实现案例
2020/06/24 Python
Css3新特性应用之视觉效果实例
2016/12/12 HTML / CSS
林清轩官方网站:山茶花润肤油开创者
2016/10/26 全球购物
行政主管职责范本
2014/03/07 职场文书
《明天,我们毕业》教学反思
2014/04/24 职场文书
群众路线剖析材料范文
2014/10/09 职场文书
个人事迹材料范文
2014/12/29 职场文书
研究生毕业论文导师评语
2014/12/31 职场文书
教师个人成长总结
2015/02/11 职场文书
你对自己的信用报告有过了解吗?
2019/07/09 职场文书
spring cloud gateway中如何读取请求参数
2021/07/15 Java/Android