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 相关文章推荐
通过ODBC连接的SQL SERVER实例
Oct 09 PHP
解决了Ajax、MySQL 和 Zend Framework 的乱码问题
Mar 03 PHP
PHP无刷新上传文件实现代码
Sep 19 PHP
PHP中如何定义和使用常量
Feb 28 PHP
PHP转换IP地址到真实地址的方法详解
Jun 09 PHP
jQuery+php实现ajax文件即时上传的详解
Jun 17 PHP
WordPress的主题编写中获取头部模板和底部模板
Dec 28 PHP
php 读取输出其他文件的实现方法
Jul 26 PHP
Laravel如何友好的修改.env配置文件详解
Jun 07 PHP
PHP实现双链表删除与插入节点的方法示例
Nov 11 PHP
laravel框架关于搜索功能的实现
Mar 15 PHP
laravel 根据不同组织加载不同视图的实现
Oct 14 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中计算时间差的几种方法
2009/12/31 PHP
QueryPath PHP 中的jQuery
2010/04/11 PHP
php 获取一个月第一天与最后一天的代码
2010/05/16 PHP
smarty模板引擎之配置文件数据和保留数据
2015/03/30 PHP
什么是OneThink oneThink后台添加插件步骤
2016/04/13 PHP
PHP获取指定时间段之间的 年,月,天,时,分,秒
2016/06/05 PHP
PHP性能分析工具xhprof的安装使用与注意事项
2017/12/19 PHP
判断控件是否已加载完成的代码
2010/02/24 Javascript
Extjs中使用extend(js继承) 的代码
2012/03/15 Javascript
Mobile Web开发基础之四--处理手机设备的横竖屏问题
2017/08/11 Javascript
利用jsonp与代理服务器方案解决跨域问题
2017/09/14 Javascript
js实现图片上传即时显示效果
2019/09/30 Javascript
JavaScript enum枚举类型定义及使用方法
2020/05/15 Javascript
[02:41]DOTA2英雄基础教程 谜团
2013/12/10 DOTA
Python读写Redis数据库操作示例
2014/03/18 Python
python实现简单socket通信的方法
2016/04/19 Python
Python金融数据可视化汇总
2017/11/17 Python
Python数字图像处理之霍夫线变换实现详解
2018/01/12 Python
python 使用sys.stdin和fileinput读入标准输入的方法
2018/10/17 Python
Python实现批量修改图片格式和大小的方法【opencv库与PIL库】
2018/12/03 Python
对pycharm 修改程序运行所需内存详解
2018/12/03 Python
python实现抖音点赞功能
2019/04/07 Python
python实现文件助手中查看微信撤回消息
2019/04/29 Python
Python依赖包整体迁移方法详解
2019/08/15 Python
Python socket非阻塞模块应用示例
2019/09/12 Python
深入浅出CSS3 background-clip,background-origin和border-image教程
2011/01/27 HTML / CSS
详解使用canvas保存网页为pdf文件支持跨域
2018/11/23 HTML / CSS
美国鞋类购物网站:Shiekh Shoes
2016/08/21 全球购物
美国知名女性服饰品牌:New York & Company
2017/03/23 全球购物
捷克建筑材料网上商店:DEK.cz
2021/03/06 全球购物
介绍一下linux文件系统分配策略
2012/11/17 面试题
abstract class和interface有什么区别
2013/08/04 面试题
运动会闭幕词
2015/01/28 职场文书
MySQL系列之六 用户与授权
2021/07/02 MySQL
MySQL七种JOIN类型小结
2021/10/24 MySQL
python turtle绘图命令及案例
2021/11/23 Python