WeChat public platform-Send passive response message-php demonstration

Source: Internet
Author: User
Tags cdata php example
Public platform-Send passive response message-php example
 Valid ();} else{$testObj->responsemsg ();}        Exit;class test{/** * Bind URL, token information */public function valid () {$echoStr = $_get["Echostr"];        if ($this->checksignature ()) {echo $echoStr;    } exit ();        }/** * Check the signature to make sure the request is from the */private function checksignature () {$signature = $_get["signature") from the sender;        $timestamp = $_get["timestamp"];        $nonce = $_get["nonce"]; $token = "test123";//consistent with the token in the configuration, non-leaking $tmparr = Array ($token, $timestamp, $nonce); sort ($TMPARR); $tmpStr = Implode ($ Tmparr); $tmpStr = SHA1 ($TMPSTR); if ($tmpStr = = $signature) {return true;}    Else{return false;}} /** * Receive messages and automatically send response information */Public function responsemsg () {//Verify signature if ($this->checksignature ()) {$po      STSTR = $GLOBALS ["Http_raw_post_data"]; $this->log_request_info (); Extract post Data if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', Libxml_nocdat                A); $fromUsername = $POSTOBJ->fromusername;//Sender $toUsername = $postObj->tousername;//receiver $MsgType = $POSTOBJ->msgtype;//Message type                                $MsgId = $POSTOBJ->msgid;//Message id $time = time ();//Current time as reply If it is a text message (the expression belongs to the text message) if ($MsgType = = ' text ') {$content = Trim ($postObj->content);//message content if (!empty ($content)) {//If the text content is graphic, then reply to the text message, otherwise reply to the textual information if ($content = = "Graphics") {//reply to the message, Articlecount text                 Number of messages, a number of text message information, the default first item is a large image $ArticleCount = 2; $NEWSTPL = "
 
   
    %s 
    
    %s 
    
   
    %s  
     %s 
    %s 
     
      <![ Cdata[%s]]>  
      %s 
      
      %s 
      
      
       
      
     
     
      <![ Cdata[%s]]>  
      %s 
      
      %s 
      
      
       
      
     
   
  
 "; $RESULTSTR = sprintf ($NEWSTPL, $fromUsername, $toUsername, $time, ' News ', $ArticleCount, ' I am graphic information ', ' I am descriptive information ', ' http://www.test.com/DocCenterService/image?photo_id=236 ', ' http://www.test.com ', ' Love City net officially launched ', ' Description 2 ', ' http:/                /jn.test.com/ac/skins/img/upload/img/20131116/48171384568991509.png ', ' http://www.test.com ');                 Echo $resultStr;                $this->log ($RESULTSTR); }else{//Reply text message $TEXTTPL = "
 
   
    
    %s 0 
    %s 
 
    
    %s 
 
    
    %s 
 
    
    %s 
 
    
 
 
 
 
 
 

  
 "; $CONTENTSTR = ' The message you send is: ' The recipient: '. $toUsername. ', sender: '. $fromUsername. ', Message type: '. $MsgType. ', message content: '. $content. '                Www.icity365.com ';                $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, ' text ', $contentStr);                Echo $resultStr;                $this->log ($RESULTSTR);                }}else{echo "Input something ...";                $this->log ($RESULTSTR); }//If it is a picture message}elseif ($MsgType = = ' image ') {$MediaId = $POSTOBJ            mediaid;//image message Media ID, you can call the multimedia file download interface to pull data. $IMAGETPL = "
 
   
   
   
 
    
    %s 
    %s 
 
    
    %s 
 
    
    %s 
 
    %s 
 
 
 
 
 
 

  
 ";                $RESULTSTR = sprintf ($IMAGETPL, $fromUsername, $toUsername, $time, $MsgType, $MediaId);            Echo $resultStr;                $this->log ("Automatic Response to Picture information");                                $this->log ($RESULTSTR); If the video}else if ($MsgType = = ' video ') {$MediaId = $POSTOBJ->mediaid;//video message Media ID, you can call the multimedia file under                Pull data from the download interface. $ThumbMediaId = $POSTOBJ The media ID of the->thumbmediaid;//video message thumbnail, you can call the multimedia file download interface to pull the data. $VIDEOTPL = "
 
  
   %s 
    %s 
 
    
    %s 
 
    
   
 
 
 
 
   %s
 
   
 
   
  
    %s
  
    
  
    %s
  
    
  
    <! [cdata[%s]]>
  
    %s
  
    
 
    

  
 "; $resultStr = sprintf ($VIDEOTPL, $fromUsername, $toUsername, $time, $MsgType, $MediaId, $ThumbMediaId, ' I am the title ', ' I am the description ');            Echo $resultStr;                $this->log ("Automatic response to video information". $ThumbMediaId);                                $this->log ($RESULTSTR);                If it is geographically}else if ($MsgType = = ' Location ') {$Location _x = $postObj->location_x;//dimension $Location _y = $postObj->location_y;//Longitude $Scale = $postObj->scale;//map Scaling Small $L Abel = $postObj->label;//field location information//reply text message $TEXTTPL = "
 
   
    
    %s 0 
    %s 
 
    
    %s 
 
    
    %s 
 
    
    %s 
 
    
 
 
 
 
 
 

  
 ";                $msgType = "text";                $contentStr = ' Longitude: '. $Location _y. ', Dimension: '. $Location _x. ', Map zoom small '. $Scale. ', location info: '. $Label;                $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);                Echo $resultStr;                                $this->log ($RESULTSTR);                                If the event}else if ($MsgType = = ' event ') {$Event = $POSTOBJ->event; Subscribe (concern, also called subscription) if ($Event = = ' Subscribe ') {$Eve Ntkey = $POSTOBJ->eventkey;//Event key value, Qrscene_ as prefix, followed by the parameter value of the QR Code//not concerned, scan QR code i F (!empty ($EventKey)) {$Ticket = $postObj->ticket;//qr code Ticket, can be used in exchange for two-dimensional code image $this->log ($f Romusername. ' Scan two-dimensional code attention!                Eventkey= '. $EventKey. ', ticket= '. $Ticket); }else{$this->log ($fromUsername. ' Follow me!                ');}//unsubscribe (Cancel attention)}elseif ($Event = = ' unsubscribe ') {$thi S->log ($fromUsername. ' Cancel my attention!                                '); have been concerned, scan QR code event}elseif ($Event = = ' Scan ' | | $Event = = ' Scan ') {$EventKey = $POSTOBJ->event                key;//Event key value, is a 32-bit unsigned integer, that is, the two-dimensional code to create a QR code scene_id $Ticket = $postObj->ticket;//Two-dimensional code Ticket, can be used to exchange QR code image $this->log ($fromUsername. ' Follow me!                                Eventkey= '. $EventKey. ', ticket= '. $Ticket); Menu click event}elseif ($Event = = ' click ') {$EventKey = $POSTOBJ->eventkey;//Event key value, with the custom menu interface key Value corresponding//reply text message $TEXTTPL = "
 
   
    
    %s 0 
    %s 
 
    
    %s 
 
    
    %s 
 
    
    %s 
 
    
 
 
 
 
 
 

  
 ";                $CONTENTSTR = ' You clicked on the menu, the item key= '. $EventKey;                $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, ' text ', $contentStr);                Echo $resultStr;                $this->log ($RESULTSTR);                Other event Types}else{$this->log (' Event Type: '. $Event); }//Other message type, link, voice, etc.}else{//reply text message $TEXTTPL = "
 
   
    
    %s 0 
    %s 
 
    
    %s 
 
    
    %s 
 
    
    %s 
 
    
 
 
 
 
 
 

  
 "; $CONTENTSTR = ' message type: '. $MsgType. ' We haven't done it yet ....                "Love the City Network";                $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, ' text ', $contentStr);                Echo $resultStr;                $this->log ($RESULTSTR);        }}else {echo "";        Exit }}else{$this->log ("Verify signature failed!    ");    }}/** * Record request information */function Log_request_info () {$post = ';    foreach ($_post as $key + $value) {$post = $post. $key. ': '. $value. ', ';} $get = '; foreach ($_get as $key = $value) {$get = $get. $key. ': '. $value. ', ';} $this->log ("GET info:". $get); $this-    >log ("Post info:". $post); /** * Log * @param $str * @param $mode */function log ($str) {$mode = ' a ';//Append write $file = "Lo    G.txt ";    $oldmask = @umask (0);    $fp = @fopen ($file, $mode);    @flock ($FP, 3);    if (! $fp) {Return false;  } else {@fwrite ($fp, $STR);      @fclose ($FP);        @umask ($oldmask);    Return true; }}}?>

?

More information view: http://mp.weixin.qq.com/wiki/index.php?title= sending a passive response message

  • 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.