WeChat development Model (PHP) _php tutorial

Source: Internet
Author: User
Tags cdata openid sprintf

Development Mode (PHP)


began to learn the development model and PHP, which is recorded for later reading.

 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 is 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 EXtern Al Entity injection, the best-on-the-is-check-validity of XML by yourself */Libxml_disab            Le_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 ' product ': $funcFlag = 0;            $CONTENTSTR = "Product link: http://www.xxx.com/products/";            $RESULTSTR = $this->transmittext ($object, $CONTENTSTR, $funcFlag);        Break            Case ' News ': $funcFlag = 0;            $CONTENTSTR = "News link: http://www.xxx.com/news/";            $RESULTSTR = $this->transmittext ($object, $CONTENTSTR, $funcFlag);        Break            Case ' scheme ': $funcFlag = 0;            $CONTENTSTR = "scheme link: http://www.xxx.com/articles/"; $RESULTSTR = $this->transmittext ($object, $CONTENTSTR, $funcFlag);            Break            Case ' we ': $funcFlag = 0;            $CONTENTSTR = "We link: 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 = "welcome you to follow XXX";        Case "unsubscribe": break; Case ' click ': Switch ($object->eventkey) {case "V1001_getcode": $contentStr = "                XXX ";            Break Case "V1002_history": $contentStr [] = Array ("Title" = "Last History", "Description" =&gt ;"                xxx "," picurl "=" xxx "," Url "and" xxx ");            Break Case "V2001_interduce": $contentStr [] =Array ("Title" = "xxx", "Description" and "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 = "unique code:" $GETUID;            } break;                    Default: $contentStr [] = Array ("Title" = "default menu reply", "Description" = "xxx",                "Picurl" = "xxx", "Url" and "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 
    %s 
 
    
    %s 
 
    
    text 
 
    
    %s 
 
                        
                     
                     
                     
                     
                     
                     

  
 ";        $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 = "
 
                      
 
   <! [cdata[%s]]>                    
 
   %s
 
                       
 
   %s
 
                       
 
   %s
 
                       

  
 ";        $item _str = ""; foreach ($arr _item as $item) $item _str. = sprintf ($ITEMTPL, $item [' Title '], $item [' Description '], $item [' Pic        URL '], $item [' url ']); $NEWSTPL = "
 
   
   
    %s
    %s $ite 
    news 
 
    
    %s 
 
    
    %s 
 
    
     
 
    M_str 
   
    %s
                        
                     
                     
                     
                     
                     

  
 "; $RESULTSTR = sprintf ($NEWSTPL, $object->fromusername, $object->tousername, Time (), COUNT ($arr _item), $funcFlag)        ;    return $resultStr;        }/* Generates a unique code */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 ']. "                [Examination not completed] ";            $flag = false; }else {return $userrow [' UserPassword ']. "                [Completed examination] ";            $flag = false; }}/* loop to determine uniqueness */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 () {//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.html www.bkjia.com true http://www.bkjia.com/PHPjc/875472.html techarticle Development Mode (PHP) began to learn the development model and PHP, which is recorded for later reading. Responsemsg ();} else {$WECHATOBJ->valid ();} Class Wechatcallbackapitest {public fu ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.