微信接收普通消息接口

时间:2024-02-18 14:23:49

官方文档地址:https://mp.weixin.qq.com/wiki/17/f298879f8fb29ab98b2f2971d42552fd.html​

当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。

请注意:

1、关于重试的消息排重,推荐使用msgid排重。
2、微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次。假如服务器无法保证在五秒内处理并回复,可以直接回复空串,微信服务器不会对此作任何处理,并且不会发起重试。详情请见“发送消息-被动回复消息”。
3、为了保证更高的安全保障,开发者可以在公众平台官网的开发者中心处设置消息加密。开启加密后,用户发来的消息会被加密,公众号被动回复用户的消息也需要加密(但开发者通过客服接口等API调用形式向用户发送消息,则不受影响)

消息类型

代码:

<?php

//
// 接收用户消息
// 微信公众账号接收到用户的消息类型判断
//
/*思路:
1.定义token
2.实例化api接口对象
3.如果有$_GET[\'echostr\'],通过验证后原样返回,用于接口验证
4.如果不存在就走responseMsg()
5.如果不存在post数据,打印出空字符串,退出
6.如果有post数据,根据post接收到的字符串将其实例化成一个XML对象
7.根据微信端XML的MsgType属性判断用户输入的信息类型,返回一个XML对象信息给用户
*/

define("TOKEN", "zhouqi");

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET[\'echostr\'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}

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;
}
}

public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, \'SimpleXMLElement\', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);

//用户发送的消息类型判断
switch ($RX_TYPE)
{
case "text": //文本消息
$result = $this->receiveText($postObj);
break;
case "image": //图片消息
$result = $this->receiveImage($postObj);
break;

case "voice": //语音消息
$result = $this->receiveVoice($postObj);
break;
case "video": //视频消息
$result = $this->receiveVideo($postObj);
break;
case "location"://位置消息
$result = $this->receiveLocation($postObj);
break;
case "link": //链接消息
$result = $this->receiveLink($postObj);
break;
default:
$result = "unknow msg type: ".$RX_TYPE;
break;
}
echo $result;
}else {
echo "";
exit;
}
}

/*
* 接收文本消息
*/
private function receiveText($object)
{
$content = "你发送的是文本,内容为:".$object->Content;
$result = $this->transmitText($object, $content);
return $result;
}

/*
* 接收图片消息
*/
private function receiveImage($object)
{
$content = "你发送的是图片,地址为:".$object->PicUrl;
$result = $this->transmitText($object, $content);
return $result;
}

/*
* 接收语音消息
*/
private function receiveVoice($object)
{
$content = "你发送的是语音,媒体ID为:".$object->MediaId;
$result = $this->transmitText($object, $content);
return $result;
}

/*
* 接收视频消息
*/
private function receiveVideo($object)
{
$content = "你发送的是视频,媒体ID为:".$object->MediaId;
$result = $this->transmitText($object, $content);
return $result;
}

/*
* 接收位置消息
*/
private function receiveLocation($object)
{
$content = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
$result = $this->transmitText($object, $content);
return $result;
}

/*
* 接收链接消息
*/
private function receiveLink($object)
{
$content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
$result = $this->transmitText($object, $content);
return $result;
}

/*
* 回复文本消息
*/
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
}
?>