简单的实现微信平台开发 使用说明:新手入门很适合的,你可以看我转载的博客:http://my.oschina.net/xz360/blog/341169 也可以去看官网的文档:http://mp.weixin.qq.com/wiki/index.php?title=%e9%a6%96%e9%a1%b5 无 ?phpdefine ( token, weixin );$wech
简单的实现微信平台开发
使用说明:新手入门很适合的,你可以看我转载的博客:http://my.oschina.net/xz360/blog/341169
也可以去看官网的文档:http://mp.weixin.qq.com/wiki/index.php?title=%e9%a6%96%e9%a1%b5 valid ();} else { $wechatobj->responsemsg ();}class wechatcallbackapitest { // 验证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 = token; $tmparr = array ( $token, $timestamp, $nonce ); sort ( $tmparr, sort_string ); $tmpstr = implode ( $tmparr ); $tmpstr = sha1 ( $tmpstr ); if ($tmpstr == $signature) { return true; } 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 event : $result = $this->receiveevent ( $postobj ); break; //接收普通的文字、图片、媒体消息 case text : $result = $this->receivetext ( $postobj ); break; } echo $result; } else { echo ; exit (); } } // 处理接收到用户发送的消息 private function receiveevent($object) { $content = ; switch ($object->event) { //关注 case subscribe : $content = 感谢您的关注,本账号目前还\n在测试阶段,给您带来的不便请谅解!; break; //取消关注 case 'unsubscribe': $content = 欢迎您再次关注我们“女人要懂得爱自己”!; break; } if (is_array ( $content )) { $result = $this->transmitnews ( $object, $content ); } else { $result = $this->transmittext ( $object, $content ); } return $result; } /* 处理接收到的文本 */ private function receivetext($object) { $keyword = trim ( $object->content ); if (strstr ( $keyword, 四六级 ) || strstr ( $keyword, 英语 )) { $content = array (); $content [] = array ( title => 2014年6月全国大学英语四六级考试成绩查询, description => , picurl => http://img003.21cnimg.com/photos/album/20141104/m600/b2579abf2b905fa793c6756a59044992.jpeg, url => http://apix.sinaapp.com/cet/index.php?openid= . $object->fromusername ); $content [] = array ( title => 刘强东带奶茶妹妹看牙 两人旁若无人地忘情缠绵(图), description => , picurl => , url => http://news.21cn.com/social/shixiang/a/2014/1105/11/28502924.shtml ); } else { $content = date ( y-m-d h:i:s, time () ) . \n目前正在测试阶段; } if (is_array ( $content )) { $result = $this->transmitnews ( $object, $content ); } else { $result = $this->transmittext ( $object, $content ); } return $result; } /* 输出文章 */ private function transmittext($object, $content) { $texttpl = %s; $result = sprintf ( $texttpl, $object->fromusername, $object->tousername, time (), $content ); return $result; } /* 输出图文 */ 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 ; $result = sprintf ( $newstpl, $object->fromusername, $object->tousername, time (), count ( $arr_item ) ); return $result; }}?>