您好,欢迎访问一九零五行业门户网

微信公共账号开发模式PHP接口实现

<?php define("token","你的token"); class wechatcallbackapi{ //public $signature; //加密签名 //public $timestamp; //时间戳 //public $nonce; //随机数 //public $echostr; //随机字符串,返回标志 public $fromusername; //发信人id public $tousername; //收信人id(本公共号) public $keywords; //文本信息 public $msgid; //消息id public $welcomemsg; //欢迎消息 public $msgtype; //消息类型 public $event; //事件名 public $mediaid; //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体 public $format; //语音格式:amr public $recognition; //语音识别结果 public function __construct(){ $this->welcomemsg="hello,欢迎订阅!"; //$this->main(); //*****************第一次验证的时候打开,随后可关闭*************** } //首次token验证 public function main(){ $echostr=$_get['echostr']; if($this->checkres()){ echo $echostr; }else{ exit(); } } //验证权限 private function checkres(){ $signature=$_get['signature']; //获取微信服务发送过来的参数 $timestamp=$_get['timestamp']; $nonce=$_get['nonce']; $token = token; $tmparr = array($token,$timestamp,$nonce); sort($tmparr); //进行字典排序 $strwaitcheck = implode($tmparr); //进行字符串组合 $strwaitcheck = sha1($strwaitcheck); //对字符串进行sha1加密 if($strwaitcheck==$signature){ return true; }else{ return false; } } public function getwhatwechatserverpost(){ $huntpostcontent = $globals["http_raw_post_data"]; //获取微信服务post过来的内容 $newxmlobj =simplexml_load_string($huntpostcontent,'simplexmlelement', libxml_nocdata); //把post过的字符串载入对象 $this->fromusername = $newxmlobj->fromusername; //向公共账号发信息的用户名 $this->tousername = $newxmlobj->tousername; //公共账号名 $this->keywords = trim($newxmlobj->content); //消息内容 $this->msgid = trim($newxmlobj->msgid); //读取当前消息msgid $this->msgtype = trim($newxmlobj->msgtype); //读取当前消息类型 $this->event = trim($newxmlobj->event); //读取当前事件名 $this->format = trim($newxmlobj->format); //语音格式:amr $this->mediaid = trim($newxmlobj->mediaid); //语音消息媒体id,可以调用多媒体文件下载接口拉取该媒体 $this->recognition = trim($newxmlobj->recognition); //语音识别结果,utf8编码 } //消息响应 public function responsemsg(){ $this->getwhatwechatserverpost(); //执行接收微信服务器post过来的东西 //当消息类型为text的时候处理 if(!empty($this->keywords)&& $this->msgtype == "text"){ $para = $this->keywords; switch($para){ case "1": $tmp_item = $this->textpicmaxtmp( "this is a test!!", "各位大叔大妈别太大较真,这就一测试滴", "http://www.baidu.com/img/bdlogo.gif", "http://www.baidu.com/"); $re_global = $this->restextpicmax($this->fromusername,$this->tousername,1,$tmp_item); echo $re_global; break; case "2": $contentrestext = "hello,buddy,you send me a \"2\"[玫瑰]"; $re_global = $this->restextmsg($this->fromusername,$this->tousername,$contentrestext); echo $re_global; break; case "000": $contentrestext = "你想返回的字符串"; $this->longstrhandleextendfunc($contentrestext); break; default: //调用功能判断函数处理 $this->longstrhandle($para); break; } }else if($this->msgtype == "event" && !empty($this->event)){ //获取事件类型 if($this->event=='subscribe'){ $contentrestext = $this->welcomemsg; //初次关注的时候推送欢迎消息 $re_global = $this->restextmsg($this->fromusername,$this->tousername,$contentrestext); }else if($this->event == "unsubscribe"){///这个地方好像没有作用!!! $contentrestext = "我擦,你咋跑了"; $re_global = $this->restextmsg($this->fromusername,$this->tousername,$contentrestext); } }else if($this->msgtype == "voice"){ if($this->recognition == ""){ $contentrestext ="亲,你说的火星文么,听不懂,建议您还是文字查询吧..[呲牙]";//语音无返回结果的时候给用记返回一条错误 $re_global = $this->restextmsg($this->fromusername,$this->tousername,$contentrestext); }else{ $contentrestext =$this->recognition; //其它情况下返回欢迎语 $re_global = $this->restextmsg($this->fromusername,$this->tousername,$contentrestext); } }else{ //如果在text情况意外得到一条空消息的处理 echo ""; exit(); } echo $re_global; //返回值给微信服务器 } //多次用到的几句话就定义了一个小方法 public function longstrhandleextendfunc($context){ $re_global = $this->restextmsg($this->fromusername,$this->tousername,$context); echo $re_global; } //收到除特定字符外的其它字符的处理方法,这个要根据你需要的具体逻辑处理 public function longstrhandle($waithandlestr){ /* your code */ } //回复文本信息方法 public function restextmsg($tu,$fu,$textcontent){ //文本消息模板 $texttpl = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[%s]]></msgtype> <content><![cdata[%s]]></content> <funcflag>0</funcflag> </xml>"; $msgtype = "text"; //消息类型 $time = time(); //时间戳 $resserverstr = sprintf($texttpl,$tu,$fu,$time,$msgtype,$textcontent); //替换模板里的内容 return $resserverstr; } //回复图文 public function restextpicmax($tu,$fu,$itemnum,$totalitemstr){ //图片模板 $pictpl = "<xml> <tousername><![cdata[%s]]></tousername> <fromusername><![cdata[%s]]></fromusername> <createtime>%s</createtime> <msgtype><![cdata[%s]]></msgtype> <articlecount>%s</articlecount> <articles>" .$totalitemstr. "</articles> </xml>"; $msgtype = "news"; //消息类型 $time = time(); //时间戳 $resserverstr = sprintf($pictpl,$this->fromusername,$this->tousername,$time,$msgtype,$itemnum); //替换模板里的内容 return $resserverstr; } //图文模板处理 public function textpicmaxtmp($title,$des,$picurl,$jumpurl){ //图文消息条目模板 $itemtpl=" <item> <title><![cdata[%s]]></title> <description><![cdata[%s]]></description> <picurl><![cdata[%s]]></picurl> <url><![cdata[%s]]></url> </item> "; $resitemstr = sprintf($itemtpl,$title,$des,$picurl,$jumpurl); //替换模板里的内容 return $resitemstr; } //接下来就是你自己定义的各种方法 }
其它类似信息

推荐信息