valid(); }else{ $testobj->responsemsg();}exit;class test{ /** * 绑定url、token信息 */ 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 = test123;//与在微信配置的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(){ //验证签名 if ($this->checksignature()){ $poststr = $globals[http_raw_post_data]; $this->log_request_info(); //提取post数据 if (!empty($poststr)){ $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); $fromusername = $postobj->fromusername;//发送人 $tousername = $postobj->tousername;//接收人 $msgtype = $postobj->msgtype;//消息类型 $msgid = $postobj->msgid;//消息id $time = time();//当前时间做为回复时间 //如果是文本消息(表情属于文本信息) if($msgtype == 'text'){ $content = trim($postobj->content);//消息内容 if(!empty( $content )){ //如果文本内容是图文,则回复图文信息,否则回复文本信息 if($content == 图文){ //回复图文消息,articlecount图文消息个数,多条图文消息信息,默认第一个item为大图 $articlecount = 2; $newstpl = %s%s; $resultstr = sprintf($newstpl, $fromusername, $tousername, $time, 'news', $articlecount,'我是图文信息','我是描述信息','http://www.test.com/doccenterservice/image?photo_id=236', 'http://www.test.com','爱城市网正式开通上线','描述2','http://jn.test.com/ac/skins/img/upload/img/20131116/48171384568991509.png', 'http://www.test.com'); echo $resultstr; $this->log($resultstr); }else{ //回复文本信息 $texttpl = %s0; $contentstr = '你发送的信息是:接收人:'.$tousername.',发送人:'.$fromusername.',消息类型:'.$msgtype.',消息内容:'.$content.' www.icity365.com'; $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, 'text', $contentstr); echo $resultstr; $this->log($resultstr); } }else{ echo input something...; $this->log($resultstr); } //如果是图片消息 }elseif ($msgtype == 'image'){ $mediaid = $postobj->mediaid;//图片消息媒体id,可以调用多媒体文件下载接口拉取数据。 $imagetpl = %s; $resultstr = sprintf($imagetpl, $fromusername, $tousername, $time, $msgtype, $mediaid); echo $resultstr; $this->log(自动响应图片信息); $this->log($resultstr); //如果是视频 }else if($msgtype == 'video'){ $mediaid = $postobj->mediaid;//视频消息媒体id,可以调用多媒体文件下载接口拉取数据。 $thumbmediaid = $postobj->thumbmediaid;//视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。 $videotpl = %s; $resultstr = sprintf($videotpl, $fromusername, $tousername, $time, $msgtype, $mediaid,$thumbmediaid,'我是标题','我是描述'); echo $resultstr; $this->log(自动响应视频信息.$thumbmediaid); $this->log($resultstr); //如果是地理位置 }else if($msgtype == 'location'){ $location_x = $postobj->location_x;//维度 $location_y = $postobj->location_y;//经度 $scale = $postobj->scale;//地图缩放大小 $label = $postobj->label;//地里位置信息 //回复文本信息 $texttpl = %s0; $msgtype = text; $contentstr = '经度:'.$location_y.',维度:'.$location_x.',地图缩放大小'.$scale.',地理位置信息:'.$label; $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr); echo $resultstr; $this->log($resultstr); //如果是事件 }else if($msgtype == 'event'){ $event = $postobj->event; //subscribe(关注,也叫订阅) if($event == 'subscribe'){ $eventkey = $postobj->eventkey;//事件key值,qrscene_为前缀,后面为二维码的参数值 //未关注时,扫描二维码 if(!empty($eventkey)){ $ticket = $postobj->ticket;//二维码的ticket,可用来换取二维码图片 $this->log($fromusername.'扫描二维码关注!eventkey='.$eventkey.',ticket='.$ticket); }else{ $this->log($fromusername.'关注我了!'); } //unsubscribe(取消关注) }elseif ($event == 'unsubscribe'){ $this->log($fromusername.'取消关注我了!'); //已关注时,扫描二维码事件 }elseif($event == 'scan' || $event == 'scan'){ $eventkey = $postobj->eventkey;//事件key值,是一个32位无符号整数,即创建二维码时的二维码scene_id $ticket = $postobj->ticket;//二维码的ticket,可用来换取二维码图片 $this->log($fromusername.'关注我了!eventkey='.$eventkey.',ticket='.$ticket); //菜单点击事件 }elseif($event == 'click'){ $eventkey = $postobj->eventkey;//事件key值,与自定义菜单接口中key值对应 //回复文本信息 $texttpl = %s0; $contentstr = '你点击了菜单,菜单项key='.$eventkey; $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, 'text', $contentstr); echo $resultstr; $this->log($resultstr); //其他事件类型 }else{ $this->log('事件类型:'.$event); } //其他消息类型,链接、语音等 }else{ //回复文本信息 $texttpl = %s0; $contentstr = '消息类型:'.$msgtype.'我们还没做处理。。。。【爱城市网】'; $resultstr = sprintf($texttpl, $fromusername, $tousername, $time, 'text', $contentstr); echo $resultstr; $this->log($resultstr); } }else { echo ; exit; } }else{ $this->log(验证签名未通过!); } } /** * 记录请求信息 */ function log_request_info() { $post = ''; foreach($_post as $key => $value) { $post = $post.$key.' : '.$value.' , '; } $get = ''; foreach($_get as $key => $value) { $get = $get.$key.' : '.$value.' , '; } $this->log(get信息:.$get); $this->log(post信息:.$post); } /** * 记录日志 * @param $str * @param $mode */ function log($str){ $mode='a';//追加方式写 $file = log.txt; $oldmask = @umask(0); $fp = @fopen($file,$mode); @flock($fp, 3); if(!$fp) { return false; } else { @fwrite($fp,$str); @fclose($fp); @umask($oldmask); return true; } } }?>
?
更多信息查看:http://mp.weixin.qq.com/wiki/index.php?title=发送被动响应消息
