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

微信公众号在开发模式下与php进行通信及回复消息

2017-07-26 00:00 232 查看
微信公众号平台在开发模式下可以在php中根据微信的对外接口,可以自定义业务逻辑的处理,今天在这里对微信公众号的订阅成功以后回复文本信息及回复图文信息进行php开发
<?php
define ( 'TOKEN', 微信公众号中的令牌Token );
$apitest = new callbackApiTest ();
$apitest->valid ();//token验证

class callbackApiTest {
/**
* 验证token
*/
public function valid() {
$echoStr = $_GET ["echostr"];//随机字符数
if ($this->checkSignature ( $echoStr )) {
echo $echoStr;
exit ();
}
}
/**
* 接收公众平台get传递的值及验证
*/
private function checkSignature($echoStr="") {
if (! defined ( "TOKEN" )) {
throw new Exception ( "TOKEN is not defined!" );
}
$nonce = $_GET ['nonce'];
$signature = $_GET ["signature"];
$timestamp = $_GET ['timestamp'];
$token = TOKEN;
$tmpArr = array (
$token,
$timestamp,
$nonce
);
// use SORT_STRING rule
sort ( $tmpArr, SORT_STRING );
$tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
////只在初次订阅的时候才会有随机码echostr的输出
if ($tmpStr == $signature && $echoStr) {
return true;
} else {
$this->reponseMsg ();
}
}

/**
* 接收事件推送
*/
public function reponseMsg() {
// 1、获取到微信推送过来post数据(xml格式)
$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
// 2、处理消息类型,并设置回复类型和内容
if (! empty ( $postArr )) {
libxml_disable_entity_loader ( true ); // 是防止XML外部实体注入,做为转化xml包的安全防御
$postObj = simplexml_load_string ( $postArr, 'SimpleXMLElement', LIBXML_NOCDATA ); // 将xml转化为对象
if (strtolower ( $postObj->MsgType ) == 'event') {
if (strtolower ( $postObj->Event ) == 'subscribe') {
$msgType = 'text';
$content = '欢迎关注公众帐号';
$this->getMsgTextTemplate($postObj,$msgType,$content);
}
//如果接收的是文本消息,按用户发送的内容自动回复消息
}elseif( strtolower( $postObj->MsgType ) == 'text' ){
switch ( trim($postObj->Content) ){
case 1:
$msgType = 'text';
$content = '你发送的消息为1';
$this->getMsgTextTemplate($postObj,$msgType,$content);
break;
case 2:
$msgType = 'text';
$content = '你发送的消息为2';
$this->getMsgTextTemplate($postObj,$msgType,$content);
break;
case '图文':
$newsArray = $this->getNewsArr();
$this->getMsgNewsTemplate($postObj, 'news', $newsArray);
break;
}
}

}
}

//获取发送TEXT文件消息
public function getMsgTextTemplate($postObj,$msgType,$content){
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$toUserName = $postObj->FromUserName;
$fromUserName = $postObj->ToUserName;
$time = time ();
$resultStr = sprintf ( $template, $toUserName, $fromUserName, $time, $msgType, $content );
echo $resultStr;
}

/**
* 获取图文数据
*/
public function getNewsArr(){
$arr = array(
array(
'title'=>'习近平出席英国女王举行的欢迎仪式',
'description'=>'进行国事访问的国家主席习近平出席了英国女王伊丽莎白二世在伦敦骑兵检阅场举行的隆重欢迎仪式。',
'picurl'=>'http://himg2.huanqiu.com/attachment2010/2015/1020/19/49/20151020074926561.jpg',
'url'=>'http://world.huanqiu.com/exclusive/2015-10/7802368.html?from=bdwz',
),
array(
'title'=>'澳大利亚一架客机与闪电“擦肩而过”瞬间',
'description'=>'澳大利亚悉尼遭遇暴风雨天气,坎塔斯航空公司的一架客机在准备降落悉尼机场时,险些被闪电击中',
'picurl'=>'http://upload.cankaoxiaoxi.com/2015/1020/thumb_114_84_1445339792572.jpg',
'url'=>'http://www.cankaoxiaoxi.com/photo/20151020/971513.shtml',
),

);
return $arr;
}
//发送图文消息
public function getMsgNewsTemplate($postObj, $msgType, $newsArray){

$template="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($newsArray)."</ArticleCount>
<Articles>";
foreach($newsArray as $k => $v){
$template.="<item>
<Title><![CDATA[".$v['title']."]]></Title>
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picurl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}
$template.="</Articles>
</xml> ";
$toUserName = $postObj->FromUserName;
$fromUserName = $postObj->ToUserName;
$time = time();
$resultStr = sprintf($template, $toUserName, $fromUserName, $time, $msgType);
echo $resultStr;
}

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