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 相关文章推荐
PHP下几种删除目录的方法总结
Aug 19 PHP
php下使用SimpleXML 处理XML 文件
Feb 27 PHP
php学习之简单计算器实现代码
Jun 09 PHP
浅析PHP程序防止ddos,dns,集群服务器攻击的解决办法
Jun 18 PHP
PHP Global定义全局变量使用说明
Aug 15 PHP
推荐10个提供免费PHP脚本下载的网站
Dec 31 PHP
php实现递归与无限分类的方法
Feb 16 PHP
PHP生成随机密码方法汇总
Aug 27 PHP
使用WAMP搭建PHP本地开发环境
May 10 PHP
PHP实现PDO操作mysql存储过程示例
Feb 13 PHP
在Laravel 中实现是否关注的示例
Oct 22 PHP
4种Windows系统下Laravel框架的开发环境安装及部署方法详解
Apr 06 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
重置版宣传动画
2020/04/09 魔兽争霸
php定界符
2014/06/19 PHP
PHP生成随机密码4种方法及性能对比
2020/12/11 PHP
许愿墙中用到的函数
2006/10/07 Javascript
js获得地址栏?问号后参数的方法
2013/08/08 Javascript
JavaScript中return false的用法
2015/03/12 Javascript
js+css实现超简洁的二级下拉菜单效果代码
2015/09/07 Javascript
原生javascript实现匀速运动动画效果
2016/02/26 Javascript
基于javascript实现样式清新图片轮播特效
2016/03/30 Javascript
原生js三级联动的简单实现代码
2016/06/07 Javascript
JS实现HTML表格排序功能
2016/08/05 Javascript
JavaScript的new date等日期函数在safari中遇到的坑
2016/10/24 Javascript
使用Vue完成一个简单的todolist的方法
2017/12/01 Javascript
js捆绑TypeScript声明文件的方法教程
2018/04/13 Javascript
Node.js 使用AngularJS的方法示例
2018/05/11 Javascript
layui table复选框禁止某几条勾选的实例
2019/09/20 Javascript
Python格式化压缩后的JS文件的方法
2015/03/05 Python
Python3.6简单操作Mysql数据库
2017/09/12 Python
Python OpenCV处理图像之图像直方图和反向投影
2018/07/10 Python
python对文件目录的操作方法实例总结
2019/06/24 Python
Pytest框架之fixture的详细使用教程
2020/04/07 Python
为什么说python适合写爬虫
2020/06/11 Python
Python如何操作docker redis过程解析
2020/08/10 Python
python使用bs4爬取boss直聘静态页面
2020/10/10 Python
python 下划线的不同用法
2020/10/24 Python
英国网上花店:Bunches
2016/11/29 全球购物
乌克兰时尚鞋子和衣服购物网站:Born2be
2018/05/24 全球购物
新浪网技术部笔试题
2016/08/26 面试题
公益广告宣传方案
2014/02/28 职场文书
房产代理公证处委托书
2014/04/04 职场文书
欢迎领导标语
2014/06/27 职场文书
交通工程专业推荐信
2014/09/06 职场文书
群众路线教育实践活动学习笔记内容
2014/11/06 职场文书
小学教师教学反思
2016/02/24 职场文书
html粘性页脚的具体使用
2022/01/18 HTML / CSS
Spring Bean是如何初始化的详解
2022/03/22 Java/Android