下面,写一个构造自动回复消息的xml类,在微信接收到消息的时候,进行自动消息回复,贴上我的代码
class We_Xml {
/**
* 构造图文消息最小为1最大为20
* 可以从外部传入图文数组变量
* @var array
*/
public $news = array (
"0" => array (
"title" => "图文消息0",//图文标题
"description" => "图文消息0",//图文描述
"picurl" => "http://www.baidu.com/xxx.jpg",//图片链接
"url" => "www.baidu.com"//图文消息地址
),
"1" => array (
"title" => "图文消息1",
"description" => "图文消息1",
"picurl" => "http://www.baidu.com/xxx.jpg",
"url" => "www.baidu.com"
),
"2" => array (
"title" => "图文消息2",
"description" => "图文消息2",
"picurl" => "http://www.baidu.com/xxx.jpg",
"url" => "www.baidu.com"
)
);
/**
* 回复文本消息
*
* @param string $toU 公众号id
* @param string $fromU 用户id
* @param string $time 时间
* @param string $msg 消息内容
* @return string
*/
public function send_Text($toU, $fromU, $time, $msg) {
$xml = "
{$time}
";
return $xml;
}
/**
* 回复图文消息
*
* @param string $toU 公众号id
* @param string $fromU 用户id
* @param string $time 时间
* @return string
*/
public function send_News($toU, $fromU, $time) {
$i=0;
$xml = "
{$time}
%s
";
$v="";
//循环构造图文消息
foreach ( $this->news as $x ) {
$c = "
";
$v = $v . $c;
$i++;
continue;
}
$v = $v . "\n\n";
$retrun = $xml . $v;
//格式化计数
$retrun=sprintf($retrun,$i);
return $retrun;
}
/**
* 回复图片消息
*
* @param string $toU 公众号id
* @param string $fromU 用户id
* @param string $time 时间
* @param string $imgid 上传媒体id
* @return string
*/
public function send_Img($toU, $fromU, $time, $imgid) {
$xml = "
{$time}
";
return $xml;
}
public function send_Video($toU, $fromU, $time, $media_id){
$xml&#61;<<
{$time}
XML;
return $xml;
}
/**
* 回复视频消息
*
* &#64;param string $toU 公众号id
* &#64;param string $fromU用户id
* &#64;param string $time 时间
* &#64;param string $media_id 上传媒体id
* &#64;param string $title 视频标题
* &#64;param string $description 视频描述
* &#64;return string
*/
public function send_Audio($toU, $fromU, $time, $media_id,$title,$description){
$xml&#61;<<
{$time}
XML;
return $xml;
}
/**
* 回复音乐消息
*
* &#64;param string $toU 公众号id
* &#64;param string $fromU 用户id
* &#64;param string $time 时间
* &#64;param string $media_id 上传媒体id
* &#64;param string $title 标题
* &#64;param string $description 描述
* &#64;param string $url 音乐地址
* &#64;param string $HQ_url 高清音乐地址&#xff0c;在WiFi环境下优先调用
* &#64;return string
*/
public function send_Music($toU, $fromU, $time, $media_id,$title,$description,$url,$HQ_url&#61;null){
//如果未添加$HQ_url则使他与$url相同
if($HQ_url&#61;null){
$HQ_url&#61;$url;
}
$xml&#61;<<
{$time}
XML;
return $xml;
}
/**
* 回复语音消息
*
* &#64;param string $toUser
* &#64;param string $fromUser
* &#64;param string $time
* &#64;param string $media_id
* &#64;return string
*/
public function send_voice($toUser,$fromUser,$time,$media_id){
$xml&#61;<<
{$time}
XML;
return $xml;
}
}
?>