PHP微信开发之微信消息自动回复下所遇到的坑
来源: 阅读:1290 次 日期:2016-08-24 14:27:49
温馨提示: 小编为您整理了“PHP微信开发之微信消息自动回复下所遇到的坑”,方便广大网友查阅!

微信回复原理:

当普通微信用户向公众账号发送消息时,微信服务器首先收到用户发送的消息;

然后将用户信息和消息打包成XML格式的数据包,再将这个XML数据包通过POST方法提交到开发者设置的URL上。

疑问一:为何使用$GLOBALS["HTTP_RAW_POST_DATA"]保存POST过来的数据,而非$_POST数组?

回答:

POST只能保存标准的数据类型,对于XML、SOAP或Application/Octet-steam之类的内容则无法解析。

而$GLOBALS["HTTP_RAW_POST_DATA"]和$_POST是一样的,如果POST过来的数据PHP能够识别,则可以用$GLOBALS["HTTP_RAW_POST_DATA"]来接收。

疑问二:simplexml_load_file()各参数和返回值是什么?

回答:

参数含义

string:需要处理的XML字符串。

class:用来指定新对象,通常设置为"SimpleXMLElement",生成一个简单XML元素的类。

options:指定附加的Libxml参数,通常设置为常量LIBXML_NOCDATA,表示把CDATA设置为文本节点。

ns:一般省略

is_prefix:一般省略

函数执行完成后返回SimpleXMLElement类的一个对象。

功能:公众号只接受文字消息,且做出相应的文字回复。

define("TOKEN","weixin");

$weixinObj = new Wechat();

$weixinObj->valid();

class Wechat{

public function valid(){

$echoStr = $_GET['echostr'];

//如果是第一次接入

if($this->checkSignature() && $echoStr ){

echo $echoStr;

exit;

}else{

$this->responseMsg();

}

}

//校验方法

private function checkSignature(){

$signature = $_GET['signature'];

$timestamp = $_GET['timestamp'];

$nonce = $_GET['nonce'];

$token = TOKEN;

$tmpArr = array($token, $timestamp, $nonce);

sort($tmpArr);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

if($tmpStr == $signature){

return true;

}else{

return false;

}

}

/* 普通文本消息

1348831860

*/

public function responseMsg(){

//获取微信服务器POST请求中的数据

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

if( !empty($postStr) ){

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUser = $postObj->FromUserName;

$toUser = $postObj->ToUserName;

$keyword = trim($postObj->Content);

$time = time();

$template = "

%s

";

if( strtolower($postObj->MsgType)!='text' ){

$msgType = "text";

$content = "我只接受文本消息";

}else{

$msgType = "text";

if( !empty($keyword) ){

$content = "您发送的消息是:".$postObj->Content;

}else{

$content = "请输入关键字";//消息为空

}

}

$info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content);

echo $info;

}else{

echo "";

exit;

}

}

}

功能:公众号只接受图片消息,且做出相应的文字回复。

define("TOKEN","weixin");

$weixinObj = new Wechat();

$weixinObj->valid();

class Wechat{

public function valid(){

$echoStr = $_GET['echostr'];

//如果是第一次接入

if($this->checkSignature() && $echoStr ){

echo $echoStr;

exit;

}else{

$this->responseMsg();

}

}

//校验方法

private function checkSignature(){

$signature = $_GET['signature'];

$timestamp = $_GET['timestamp'];

$nonce = $_GET['nonce'];

$token = TOKEN;

$tmpArr = array($token, $timestamp, $nonce);

sort($tmpArr);

$tmpStr = implode($tmpArr);

$tmpStr = sha1($tmpStr);

if($tmpStr == $signature){

return true;

}else{

return false;

}

}

/* 接收图片消息格式

1348831860

1234567890123456

*/

public function responseMsg(){

//获取微信服务器POST请求中的数据

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

if( !empty($postStr) ){

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUser = $postObj->FromUserName;

$toUser = $postObj->ToUserName;

$time = time();

$msgType= $postObj->MsgType;

$picUrl = $postObj->PicUrl;

$mediaId = $postObj->MediaId;

$template = "

%s

";

if( strtolower($msgType)!='image' ){

$msgType = "text";

$content = "我只接受图片消息";

}else{

$msgType = "text";

if( !empty( $picUrl ) ){

$content = "图片链接为:".$picUrl."\n";

$content .= "媒体id:".$mediaId;

}else{

$content = "请发送图片";//消息为空

}

}

$info = sprintf($template, $fromUser, $toUser, $time, $msgType, $content);

echo $info;

}else{

echo "";

exit;

}

}

}

以上是小编给大家分享的微信消息自动回复下所遇到的坑的相关知识,希望对大家有所帮助!

更多信息请查看 网络编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2026国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们| 联系我们| 人才招聘| 网站声明| 网站帮助| 非正式的简要咨询| 简要咨询须知| 新媒体/短视频平台| 手机站点| 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:
咨询QQ:1093837350(9:00—18:00) 版权所有:
云南网警报警专用图标
Baidu
map