speedphp micro-letter interface extension

Source: Internet
Author: User
Tags array cdata empty interface variables sha1 sha1 encryption sort
This extension realizes the docking of the SP and the micro-trust public platform, and the 1.0 version only realizes the simplest function: binding, receiving information and replying information.

To extend the configuration method:

$spConfig = Array (
' Mode ' => ' Debug '
Extended Configuration
, ' ext ' => array (
Micro-Credit Extension settings
' Spweixin ' => Array (
' TOKEN ' => ' weixin '/micro-letter communication key, background setting
)
)
);
 
And then execute it in the Controll method:
$wx = Spclass (' spweixin ');
$msg = $wx->run ();

On the line, if it is binding then directly output the micro-letter specifies the display of the ECHOSTR, the program aborted execution. If the message is received, then $msg is the array of received information, and the key name is in contrast to the official document of the micro-letter.

 

<?php  /**  * <b>speedphp Micro-interface extension </b>  * <p>2013 year August 15 08:51:23</p>  * @ Author Lee (leeldy[at]163.com)  * @version 1.0  */class Spweixin {     //micro-letter communication key     p Rivate $token;    /micro-credit public account ID     private $publicuserid;    /Information recipient     private $touserid;       Public Function __construct () {        $params = Spext (' spweixin ');   &NBSP ;    //Get configuration Parameters         if (Empty ($params [' TOKEN ']) {          &NBSP ; Sperror (' micro-credit public account communication key is not set! ');        }         $this->token = $params [' token '];    }      /**      * binding micro-interface      * @return stringfalse   &NB Sp  */    Public Function bind () {         //random string         $EC Hostr = $_get["Echostr"];        /micro-letter encryption signature         $signature = $_get["signature"];        //Signature time stamp         $timestamp = $_get["timestamp"];        //encryption random number         $nonce = $_get["nonce"];           *          * encryption/verification process:           1. Sort the token, timestamp, nonce three parameters in dictionary order           2. Concatenation of three parameter strings into a string for SHA1 encryption           3. The developer obtains the encrypted string to compare with the signature, identifies the request from the micro-letter          */        $TMPARR = Array ($thi S->token, $timestamp, $nonce);         sort ($TMPARR);         $_signature = SHA1 (implode ($TMPARR));           if ($_signature!= $signature) {           ///signature incorrect, return FA LSE             $ECHOSTRFalse        }           return $echoStr;    }      /**      * receiving messages, returning message array      * @return Array   &NBS P  */    Public function receive () {        $raw = $this->php_fix_raw_query (); &nbsp ;       $msg = false;         if (!empty ($raw)) {            $msg = (array) simplexml_load_st Ring ($GLOBALS [' Http_raw_post_data '], ' simplexmlelement ', libxml_nocdata);             $this->publicuserid = $msg [' Tousername '];             $this->touserid = $msg [' Fromusername '];        }           return $msg;    }      /**      * get raw data for POST submission      * @author robotreply at Gmail dot com (24-jul-2009 08:17)      * @rEturn string      */    Private Function php_fix_raw_query () {        &NBSP ; Try Globals array        //attempt to get         isset from global variables ($GLOBALS ["Http_raw_pos T_data "]) {            return $GLOBALS [" Http_raw_post_data "];                //Try globals variable        //try to get         from global variables if (Isset ($HTTP _raw_post_data)) {            return $HTTP _raw_post_data     &NB Sp             $post = ';        //try Stream        //attempt to get         if (!function_) from stream Exists (' file_get_contents ')) {           //php://input is isn't available with enctype= mult Ipart/form-data ".             $FP = fopen ("Php://input"," R ");             if ($fp) {               !feof ( $FP)) {                    $post = fread ($fp, 1024);     &NBS P           {                  fclose ($FP);                    } else {            $post = file_get_contents ("Php://input");        }           return $post;    }      /**      * <b> reply text message </b>      * <p> for Each POST request, the developer returns a specific XML structure in response subpackages </p>      * <p> responds to the message (now supports reply text, graphics, voice, video, music, and star operations on incoming messages) </p>      * <p> Micro-trust server within five seconds of receiving a response will break the connection. </p>      * @param string $Content   reply message content      *@return stringfalse      */    Public Function Replytext ($Content) {        $msg = false;           if (!empty ($Content)) {           //createtime message creation time & nbsp           $CreateTime = time ();               $msg = <<<xml <xml>     <tousername><![ cdata[{$this->touserid}]]></tousername>     <fromusername><! [cdata[{$this->publicuserid}]]></fromusername>     <createtime>{$CreateTime}</ createtime>     <msgtype><! [cdata[text]]></msgtype>     <content><! [cdata[{$Content}]]></content> </xml> xml;        }           return $msg;    }      /**      * <b> reply music message </b>     * @param string $Title title      * @param string $Description description      * @param string $M Usicurl Music links      * @param string $HQMusicUrl High quality music links, WiFi environments Use this link first to play music      * @return Strin Gfalse      */    Public Function replymusic ($Title, $Description, $MUSICURL, $HQMusicUrl) {&NB Sp       $msg = false;           if (!empty ($MUSICURL)) {           //createtime message creation time             $CreateTime = time ();               $msg = <<<xml <xml>     <tousername><![ cdata[{$this->touserid}]]></tousername>     <fromusername><! [cdata[{$this->publicuserid}]]></fromusername>     <createtime>{$CreateTime}</ createtime>     <msgtype><! [cdata[music]]></msgtype> &NBSp   <Music>         <title><! [cdata[{$Title}]]></title>         <description><! [cdata[{$Description}]]></description>         <musicurl><! [cdata[{$MusicUrl}]]></musicurl>        

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.