微信开发基本消息回复

来源:互联网 发布:军萝脸型数据 编辑:程序博客网 时间:2024/06/05 09:20
<?php
define(TOKEN, "weixin");
define("AppID","");
define("EncodingAESKey","");
require_once('wxBizMsgCrypt.php');


$wechatObj=new wechatCallbackapiTest();
if(!isset($_GET['echostr'])){
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}


class wechatCallbackapiTest{
//验证签名
public function valid(){
$echostr=$_GET["echostr"];
$signature=$_GET["signature"];
$timestamp=$_GET["timestamp"];
$nonce=$_GET["nonce"];
$tmpArr=array(TOKEN,$timestamp,$nonce);
sort($tmpArr);
$tmpStr=implode($tmpArr);
$tmpStr=sha1($tmpStr);
if ($tmpStr==$signature) {
echo $echostr;
exit;
}


}
//响应消息
public function responseMsg(){
$timestamp=$_GET["timestamp"];
$nonce=$_GET["nonce"];
$msg_signature=$_GET['msg_signature'];
$encrypt_type=(isset($_GET['encrypt_type'])&&
($_GET['encrypt_type']=='aes'))?"aes":"raw";
$postStr=$GLOBALES["HTTP_RAW_POST_DATA"];
if(!empty($postStr)){
//解密
if ($encrypt_type=='aes') {
$pc=new wxBizMsgCrypt(TOKEN,EncodingAESKey,AppID);
$this->logger("D \r\n".$postStr);
$decryptMsg="";//解密后的明文
$errCode=$pc->DecryptMsg($msg_signature,$timestamp,$nonce,$postStr,$decryptMsg);
$postStr=$decryptMsg;


}
$this->logger("R \r\n".$postStr);
$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$RX_TYPE=trim($postObj->MsgType);//消息类型
//消息类型分离
switch($RX_TYPE){
case 'event':
$result=$this->receiveEvent($postObj);
break;
case 'text':
$result=$this->receiveText($postObj);
break;
}
$this->logger("R \r\n".$result);
//加密
if($encrypt_type=='aes'){
$encryptMsg='';//加密后的密文
$errCode=$pc->encryptMsg($result,$timestamp,$nonce,$encryptMsg);
$result=$encryptMsg;
$this->logger("E \r\n".$result);
}
echo $result;
}else{
echo "";
exit;
}
}
//接收文本消息
private function receiveText($object){
$keyword=trim($object->Content);
if(strstr($keyword, "文本")){
$content="这是个文本消息";
}else if(strstr($keyword,"单图文")){
$content=array();
$content[]=array("Title"=>"单图文标题","Description"=>"单图文内容","PicUrl"=>"图片地址","Url"="链接地址");
}else if (strstr($keyword,"图文"||$keyword=="多图文")) {
$content=array();
$content[]=array("Title"=>"多图文1标题","Description"=>"","PicUrl"=>"","Url"=>"");
$content[]=array("Title"=>"多图文2标题","Description"=>"","PicUrl"=>"","Url"=>"");
$content[]=array("Title"=>"多图文3标题","Description"=>"","PicUrl"=>"","Url"=>"");
}else if (strstr($keyword,"音乐")) {
$content=array();
$content[]=array("Title"=>"三天三夜","Description"=>"歌手:歌浴森","MusicUrl"=>"","Url"=>"");
}else{
$content=date("Y-m-d H:i:s",time()."\n".$object->FromUserName."\n H5edu")
}
if(is_array($content)){
if(isset($content[0])){
$result=$this->transmitNews($object,$content);
}else if(isset($content['MusicUrl'])){
$result=$this->transmitMusic($object,$content);
}else{
$result=$this->transmitText($object,$content);
}
return $result;
}
}
//回复文本消息
private function transmitText($object,$content){
$xmlTpl="<xml>
<ToUserName>![CDATA[%s]]</ToUserName>
<FromUserName>![CDATA[%s]]</FromUserName>
<CreateTime>%s</CreateTime>
<MsgType>![CDATA[%s]]</MsgType>
<Content>![CDATA[$s]]</Content>
</xml>";
$result=sprintf($xmlTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
return result;
}
//回复图文消息
private function transmitNews($object,$newsArray){
if(!is_array($newsArray)){
return;
}
$itemTpl="
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]><PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$item_str="";
foreach ($newsArray as $item) {
$item_str.=sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url'])
}
$xmlTpl="
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%S]]><FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]><MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>$item_str</Articles>
";
$result=sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time(),count($newsArray));
return $result;
}


//回复音乐信息
private function transmitMusic($object,$MusicArray){
$itemTpl="<Music>
<Title><![CDATA[$s]]></Title>
<Description><![![CDATA[$s]]></Description>
<MusicUrl><![!CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[music]]></HQMusicUrl>
$item_str;
</Music>";
$result=sprintf($xmlTpl,$object->FromUserName,$object->ToUserName,time());
return $result;
}


//日志记录
public function logger($log_content){
if(isset($_SERVER['HTTP_APPNAME'])){//sae
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else if($_SERVER['REMOTE_ADDR']!="127.0.0.1"){//LOCAL
$max_size=500000;
$log_filename="log.xml";
if(file_exists($log_filename)and(abs(filesize($log_filename))>$max_size)){
unlink($log_filename);
}
file_put_contents($log_filename, date("Y-m-d H:i:s").$log_content."/r/n",FILE_APPEND);
}
}
}
?>
原创粉丝点击