您的位置:首页 > 移动开发 > 微信开发

让公众号直接返回微信客户端输入的文本内容

2016-11-10 21:58 274 查看
    根据青城老贼的文章进行实践,让公众号直接返回输入的文本内容。原文:http://www.zhongyaofang.cn/combat_power/weixin_jiaocheng_vol11.html



修改wx_token_read.php的内容:

<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="php"><?php
//装载模板文件
include_once("wx_tpl.php");

//获取微信发送数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

//返回回复数据
if (!empty($postStr)){

//解析数据
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//发送消息方ID
$fromUsername = $postObj->FromUserName;
//接收消息方ID
$toUsername = $postObj->ToUserName;
//消息类型
$form_MsgType = $postObj->MsgType;

//事件消息
if($form_MsgType=="text")
{
//获取用户发送的文字内容
$form_Content = trim($postObj->Content);
//
if(!empty($form_Content))
{
// 若非空,直接回复
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $form_Content);
echo $resultStr;
exit;
}
else
{
// 若空,提示输入
$msgType = "text";
//$contentStr = $form_Content;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, "输入文字试试看");
echo $resultStr;
exit;

}

//$logfile = fopen("log.txt", "w") or die("Unable to open file!");
//fwrite($logfile, $resultStr);
//fclose($logfile);

}
elseif($form_MsgType=="event")// 接收到事件
{
//获取事件类型
$form_Event = $postObj->Event;
//订阅事件
if($form_Event=="subscribe")
{
//回复欢迎文字消息
$msgType = "text";
$contentStr = "感谢您关注老狼的微信测试![愉快]\n\n想学公众平台使用的朋友请输入“跟我学”![玫瑰]";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $contentStr);
echo $resultStr;
exit;
}

}

}
else
{
echo "";
exit;
}

?>




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微信 公众号 回复
相关文章推荐