一、统一以文本的方式回复各种类型消息
1.截图
2.实现代码
<?php
/**
* 基础接口之接收用户消息思路:
* 1.首先,微信公众账号对接收到的用户的消息类型进行判断
* 2.然后根据发送过来的7种消息分别做相应的业务处理
* 3.本实验业务处理的思路是统一以文本的方式回复各种类型消息的核心内容
*/
error_reporting(0);
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();//实例化类wechatCallbackapiTest
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
/**
*声明一个类 wechatCallbackapiTest,该类中包含有三个方法(函数)。
* valid,responseMsg,checkSignature
* valid和checkSignature主要是用来验证用的,responseMsg则是用来回复用户消息的
*/
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为用户在微信公众平台发的信息
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
//simplexml_load_string为——转换形式良好的 XML 字符串为 SimpleXMLElement 对象,然后输出对象的键和元素
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);//判断平台接受的消息类型
//用户发送的消息类型判断
switch ($RX_TYPE)
{
case "text": //文本消息
$resultStr = $this->receiveText($postObj);
break;
case "image": //图片消息
$resultStr = $this->receiveImage($postObj);
break;
case "voice": //语音消息
$resultStr = $this->receiveVoice($postObj);
break;
case "video": //视频消息
$resultStr = $this->receiveVideo($postObj);
break;
case "location"://位置消息
$resultStr = $this->receiveLocation($postObj);
break;
case "link": //链接消息
$resultStr = $this->receiveLink($postObj);
break;
default:
$resultStr = "unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else {
echo "???";
exit;
}
}
//上面与前一篇相同,主要代码在这里开始
//text,image,voice,video,shortvideo,location,link.
//1.接收文本消息
private function receiveText($object)
{
$contentStr = "你发送的是文本,内容为:".$object->Content;
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
//2.接收图片消息
private function receiveImage($object)
{
$contentStr = "你发送的是图片,地址为:".$object->PicUrl;
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
//3.接收语音消息
private function receiveVoice($object)
{
$contentStr = array("MediaId"=>$object->MediaId);
$resultStr = $this->transmitVoice($object, $contentStr);;
return $resultStr;
}
//4.接收视频/短视频消息
private function receiveVideo($object)
{
$contentStr = "你发送的是视频,媒体ID为:".$object->MediaId;
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
//5.接收位置消息
private function receiveLocation($object)
{
$contentStr = "你发送的是位置,纬度为:".$object->Location_X.
";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
//6.接收链接消息
private function receiveLink($object)
{
$contentStr = "你发送的是链接,标题为:".$object->Title.
";内容为:".$object->Description.";链接地址为:".$object->Url;
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
//被动回复信息函数:文本和语音
//回复文本消息
private function transmitText($object, $content)
{
//4个%s;
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
//sprintf——格式化xml模板
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $resultStr;
}
//回复语音消息
private function transmitVoice($object, $voiceArray)
{
$itemTpl = "<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>";
$item_str = sprintf($itemTpl, $voiceArray['MediaId']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
$item_str
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $resultStr;
}
}
?>
写完代码后需要更新到新浪云上,并且确保测试号的接口配置信息是你的现在所写的这个文件
可以现在此调试器上进行测试,比较方便
微信调试器-方培工作室:http://www.fangbei.org/tool/message
3.解释:重点在组装xml数据
接受用户发给测试号的信息,判断其不同的信息类型,根据它们各自的xml数据,提取其中所需要显示的信息,
例如: $object->PicUrl,$object->Content...
等这些信息
然后传给组装xml数据回复的函数去进行组装,用以回复用户。
其余类似
注:若要实现回复语音需要在你的测试号开启“接受语音识别结果”
transmit:传输,传送
receive:接受,接收
接受普通信息的xml数据:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html
被动回复用户信息的xml数据:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html
2020-06-12