微信公众平台开发模式下如何设置图文回复
发布网友
发布时间:2022-06-02 04:08
我来回答
共2个回答
热心网友
时间:2023-10-28 09:24
<?php
/*
回复不同的消息 包括文字,图片,图文,链接等
*/
define("TOKEN", "pondbay");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->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 = $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":
$resultStr = $this->receiveText($postObj);
break;
}
echo $resultStr;
}else {
echo "";
exit;
}
}
private function receiveText($object)
{
$funcFlag = 0;
$keyword = trim($object->Content);
$resultStr = "";
$contentStr = "";
if($keyword == "文本"){
$contentStr = "这是个文本消息";
$resultStr = $this->transmitText($object, $contentStr, $funcFlag);
}
else if($keyword == "图文" || $keyword == "单图文"){
$dateArray = array();
$dateArray[] = array("Title"=>"单图文标题",
"Description"=>"单图文内容",
"Picurl"=>"图片地址(你自己的)",
"Url" =>"链接(你自己的)");
$resultStr = $this->transmitNews($object, $dateArray, $funcFlag);
}
else if($keyword == "多图文"){
$dateArray = array();
$dateArray[] = array("Title"=>"多图文1标题", "Description"=>"", "Picurl"=>"图片地址(你自己的)", "Url" =>"链接(你自己的)");
$dateArray[] = array("Title"=>"多图文2标题", "Description"=>"", "Picurl"=>"图片地址(你自己的)", "Url" =>"链接(你自己的)");
$dateArray[] = array("Title"=>"多图文3标题", "Description"=>"", "Picurl"=>"图片地址(你自己的)", "Url" =>"链接(你自己的)");
$resultStr = $this->transmitNews($object, $dateArray, $funcFlag);
}
else if($keyword == "音乐"){
$musicArray = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3","HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
$resultStr = $this->transmitMusic($object, $musicArray, $funcFlag);
}
return $resultStr;
}
private function receiveEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "欢迎关注方倍工作室";
break;
}
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
private function transmitText($object, $content, $flag = 0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
private function transmitNews($object, $arr_item, $flag = 0)
{
if(!is_array($arr_item))
return;
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['Picurl'], $item['Url']);
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
<FuncFlag>%s</FuncFlag>
</xml>";
$resultStr = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $flag);
return $resultStr;
}
private function transmitMusic($object, $musicArray, $flag = 0)
{
$itemTpl = "<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $flag);
return $resultStr;
}
}
?>
热心网友
时间:2023-10-28 09:24
去百度经验找