php公众号消息接收和发送操作代码
代码语言:php
所属分类:会话
代码描述:php公众号消息接收和发送操作代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
/**
* 微信公众平台操作类
*/
class WeChat
{
private $_appid;
private $_appsecret;
//微信公众平台请求开发者的服务器需要token
private $_token;
//标识qrcodeticket的类型,是永久还是临时
const QRCODE_TYPE_TEMP = 1;
const QRCODE_TYPE_TEMP_STR = 2;
const QRCODE_TYPE_LIMIT = 3;
const QRCODE_TYPE_LIMIT_STR = 4;
/**
* 构造函数
* @param string $id appid
* @param string $secret app秘钥
*/
public function __construct($id, $secret, $token) {
$this->_appid = $id;
$this->_appsecret = $secret;
$this->_token = $token;
}
/**
* 用于第一次验证URl合法性
*/
public function firstValid() {
//校验签名的合法性
if ($this->_checkSignature()) {
//签名合法,告知微信服务器
echo $_GET['echostr'];
}
}
/**
* 验证签名
* @return [type] [description]
*/
private function _checkSignature() {
//获取由微信服务器发过来的数据
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
//开始验证数据
$tmp_arr = array($this->_token, $timestamp, $nonce);
sort($tmp_arr, SORT_STRING);
$tmp_str = implode($tmp_arr);
$tmp_str = sha1($tmp_str);
//对比数据
if ($signature == $tmp_str) {
return true;
} else {
return false;
}
}
/**
* 消息类型判断
* @return array
*/
public function responseMsg() {
//因为很多都设置了register_globals禁止,不能用$GLOBALS["HTTP_RAW_POST_DATA"] 改用file_get_contents("php://input")即可
$postStr = file_get_contents("php://input");
if (!empty($postStr)) {
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
//用户发送的消息类型判断
switch ($RX_TYPE) {
case "text": //文本消息
return array('type' => 'text', 'm.........完整代码请登录后点击上方下载按钮下载查看









网友评论0