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

微信公众平台图文模式测试

2017-01-07 21:03 246 查看
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

//extract post data
if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount><![CDATA[3]]></ArticleCount>
<Articles>
<item>
<Title><![CDATA[haha,mt1]]></Title>
<Description><![CDATA[test1]]></Description>
<PicUrl><![CDATA[http://www.test.com/test.jpg]]></PicUrl>
<Url><![CDATA[http://www.baidu.com]]></Url>
</item>
<item>
<Title><![CDATA[haha,mt2]]></Title>
<Description><![CDATA[test1]]></Description>
</item>
<item>
<Title><![CDATA[haha,mt3]]></Title>
<Description><![CDATA[test1]]></Description>
</item>
</Articles>

</xml>";
if(!empty( $keyword ))
{
$msgType="news";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType);
echo $resultStr;
}else{
echo "Input something...";
}

}else {
echo "";
exit;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: