微信开发模式(php) 才开始学习微信开发模式和php,在此记录以供后阅。
responsemsg();}else { $wechatobj->valid();}class wechatcallbackapitest { public function valid() { $echostr = $_get[echostr]; //valid signature , option if ( $this->checksignature() ) { echo $echostr; exit; } } public function responsemsg() { //get post data, may be due to the different environments $poststr = $globals[http_raw_post_data]; //extract post data if ( !empty( $poststr ) ) { /* libxml_disable_entity_loader is to prevent xml external entity injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader( true ); $postobj = simplexml_load_string( $poststr, 'simplexmlelement', libxml_nocdata ); $rx_type = trim( $postobj->msgtype ); switch ( $rx_type ) { case text: $resultstr = $this->receivetext( $postobj ); break; case event: $resultstr = $this->receiveevent( $postobj ); break; default: $resultstr = ; break; } echo $resultstr; } else { echo ; exit; } } private function receivetext( $object ) { $getkeyword = $object->content; switch ( $getkeyword ) { case '产品': $funcflag = 0; $contentstr = 产品链接:http://www.xxx.com/products/; $resultstr = $this->transmittext( $object, $contentstr, $funcflag ); break; case '新闻': $funcflag = 0; $contentstr =新闻链接:http://www.xxx.com/news/; $resultstr = $this->transmittext( $object, $contentstr, $funcflag ); break; case '方案': $funcflag = 0; $contentstr =方案链接:http://www.xxx.com/articles/; $resultstr = $this->transmittext( $object, $contentstr, $funcflag ); break; case '我们': $funcflag = 0; $contentstr =我们链接:http://www.xxx.com/culture/?type=detail&id=1; $resultstr = $this->transmittext( $object, $contentstr, $funcflag ); break; default: break; } return $resultstr; } private function receiveevent( $object ) { $contentstr = ; switch ( $object->event ) { case subscribe: $contentstr = 欢迎您关注xxx; case unsubscribe: break; case click: switch ( $object->eventkey ) { case v1001_getcode: $contentstr = xxx; break; case v1002_history: $contentstr[] = array( title =>最后一条历史记录, description =>xxx, picurl =>xxx, url =>xxx ); break; case v2001_interduce: $contentstr[] = array( title =>xxx, description =>xxx, url =>http://mp.weixin.qq.com/xxx ); break; case v3001_join: $getuid = $this->getuid( $object ); if ( $getuid==1 ) { $contentstr = 'could not connect: '.mysql_error(); }else { $contentstr = 唯一码:.$getuid; } break; default: $contentstr[] = array( title =>默认菜单回复, description =>xxx, picurl =>xxx, url =>xxx ); break; } break; default: break; } if ( is_array( $contentstr ) ) { $resultstr = $this->transmitnews( $object, $contentstr ); }else { $resultstr = $this->transmittext( $object, $contentstr ); } return $resultstr; } private function transmittext( $object, $content, $funcflag = 0 ) { $texttpl = %s %d ; $resultstr = sprintf( $texttpl, $object->fromusername, $object->tousername, time(), $content, $funcflag ); return $resultstr; } private function transmitnews( $object, $arr_item, $funcflag = 0 ) { 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 %s ; $resultstr = sprintf( $newstpl, $object->fromusername, $object->tousername, time(), count( $arr_item ), $funcflag ); return $resultstr; } /* 生成唯一码*/ public function getuid( $object ) { $getopenid = $object->fromusername; $con = mysql_connect( localhost, root, xxx ); mysql_select_db( xxx, $con ); if ( !$con ) { return 1; } $flag = true; $getuserresult = mysql_query( select * from loginuser where openid = '.$getopenid.' ); $getuserrow = mysql_num_rows( $getuserresult ); if ( $getuserrow>0 ) { $userrow = mysql_fetch_array( $getuserresult ); if ( $userrow['state']==0 ) { return $userrow['userpassword'].[未完成考试]; $flag = false; }else { return $userrow['userpassword'].[已完成考试]; $flag = false; } } /* 循环确定唯一性 */ while ( $flag ) { $uid = uniqid( true ); $uid = substr( $uid, -8 ); $result = mysql_query( select * from loginuser where userpassword = '.$uid.' ); $row = mysql_num_rows( $result ); if ( $row == 0 ) { mysql_query( insert into loginuser (userpassword,state,openid) values ('.$uid.','0','.$getopenid.') ); mysql_close( $con ); return $uid; $flag =false; } } } private function getuidtest( $object ) { return $object->fromusername; } private function checksignature() { // you must define token by yourself if ( !defined( token ) ) { throw new exception( 'token is not defined!' ); } $signature = $_get[signature]; $timestamp = $_get[timestamp]; $nonce = $_get[nonce]; $token = token; $tmparr = array( $token, $timestamp, $nonce ); // use sort_string rule sort( $tmparr, sort_string ); $tmpstr = implode( $tmparr ); $tmpstr = sha1( $tmpstr ); if ( $tmpstr == $signature ) { return true; }else { return false; } }}?>
http://www.bkjia.com/phpjc/875472.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/875472.htmltecharticle微信开发模式(php) 才开始学习微信开发模式和php,在此记录以供后阅。 responsemsg();}else { $wechatobj->valid();}class wechatcallbackapitest { public fu...