微信公众平台开发入门--php,实现自动回复文本,图文,点击事件
一页代码实现微信基本回复和点击事件功能,部署上去sae或者bae,妥妥的基本免费的服务器
不懂代码都基本每个人都可以做自己的微信公众号了
valid();}else{ //回复消息 $wechatobj->responsemsg();}class wechatcallbackapitest{ 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 = 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() { $poststr = $globals[http_raw_post_data]; if (!empty($poststr)){ $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); $rx_type = trim($postobj->msgtype); switch ($rx_type) { case text: $resultstr = $this->receivetext($postobj); break; case image: $resultstr = $this->receiveimage($postobj); break; case location: $resultstr = $this->receivelocation($postobj); break; case voice: $resultstr = $this->receivevoice($postobj); break; case video: $resultstr = $this->receivevideo($postobj); break; case link: $resultstr = $this->receivelink($postobj); break; case event: $resultstr = $this->receiveevent($postobj); break; default: $resultstr = unknow msg type: .$rx_type; break; } echo $resultstr; }else { echo ; exit; } } //接收文本消息 private function receivetext($object) { $keyword = trim($object->content); $url = http://api100.duapp.com/movie/?appkey=diy_miaomiao&name=.$keyword; $output = file_get_contents($url,$keyword); $contentstr = json_decode($output, true); if (is_array($contentstr)){ $resultstr = $this->transmitnews($object, $contentstr); }else{ $resultstr = $this->transmittext($object, $contentstr); } return $resultstr; } //接收事件,关注等 private function receiveevent($object) { $contentstr = ; switch ($object->event) { case subscribe: $contentstr = 你关注了我; //关注后回复内容 break; case unsubscribe: $contentstr = ; break; case click: $contentstr = $this->receiveclick($object); //点击事件 break; default: $contentstr = receive a new event: .$object->event; break; } return $contentstr; } //接收图片 private function receiveimage($object) { $contentstr = 你发送的是图片,地址为:.$object->picurl; $resultstr = $this->transmittext($object, $contentstr); return $resultstr; } //接收语音 private function receivevoice($object) { $contentstr = 你发送的是语音,媒体id为:.$object->mediaid; $resultstr = $this->transmittext($object, $contentstr); return $resultstr; } //接收视频 private function receivevideo($object) { $contentstr = 你发送的是视频,媒体id为:.$object->mediaid; $resultstr = $this->transmittext($object, $contentstr); return $resultstr; } //位置消息 private function receivelocation($object) { $contentstr = 你发送的是位置,纬度为:.$object->location_x.;经度为:.$object->location_y.;缩放级别为:.$object->scale.;位置为:.$object->label; $resultstr = $this->transmittext($object, $contentstr); return $resultstr; } //链接消息 private function receivelink($object) { $contentstr = 你发送的是链接,标题为:.$object->title.;内容为:.$object->description.;链接地址为:.$object->url; $resultstr = $this->transmittext($object, $contentstr); return $resultstr; } //点击菜单消息 private function receiveclick($object) { switch ($object->eventkey) { case 1: $contentstr = 猫咪酱个性diy服装,我们专业定制个性【班服,情侣装,亲子装等,有长短t恤,卫衣,长短裤】 来图印制即可,给你温馨可爱的ta,有事可直接留言微信; break; case 2: $contentstr = 你点击了菜单: .$object->eventkey; break; case 3: $contentstr = 是傻逼; break; default: $contentstr = 你点击了菜单: .$object->eventkey; break; } //两种回复 if (is_array($contentstr)){ $resultstr = $this->transmitnews($object, $contentstr); }else{ $resultstr = $this->transmittext($object, $contentstr); } return $resultstr; } //回复文本消息 private function transmittext($object, $content) { $texttpl = %s ; $resultstr = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $content); return $resultstr; }
//回复图文 private function transmitnews($object, $arr_item) { if(!is_array($arr_item)) return;
$itemtpl = ; $item_str = ; foreach ($arr_item as $item) $item_str .= sprintf($itemtpl, $item['title'], $item['description'], $item['picurl'], $item['url']);
$newstpl = %s %s $item_str ;
$resultstr = sprintf($newstpl, $object->fromusername, $object->tousername, time(), count($arr_item)); return $resultstr; } //音乐消息 private function transmitmusic($object, $musicarray, $flag = 0) { $itemtpl = ;
$item_str = sprintf($itemtpl, $musicarray['title'], $musicarray['description'], $musicarray['musicurl'], $musicarray['hqmusicurl']);
$texttpl = %s $item_str %d ;
$resultstr = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $flag); return $resultstr; } }?>