当前位置:首页 > PHP教程 > php高级应用 > 列表

php微信开发之关注事件

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-14 15:27:22 浏览: 评论:0 

本文实例为大家分享了php微信关注事件的具体代码,供大家参考,具体内容如下:

  1. <!--?php 
  2. /** 
  3.  * wechat php test 
  4.  */ 
  5.   
  6. //define your token 
  7. define("TOKEN", "weixin"); 
  8. $wechatObj = new wechatCallbackapiTest(); 
  9. //$wechatObj--->valid(); 
  10. $wechatObj->responseMsg(); 
  11. class wechatCallbackapiTest 
  12. { 
  13.   public function valid() 
  14.   { 
  15.     $echoStr = $_GET["echostr"]; 
  16.   
  17.     //valid signature , option 
  18.     if($this->checkSignature()){ 
  19.       echo $echoStr; 
  20.       exit; 
  21.     } 
  22.   } 
  23.   
  24.   public function responseMsg() 
  25.   { 
  26.     //get post data, May be due to the different environments 
  27.     $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  28.   
  29.     //extract post data 
  30.     if (!emptyempty($postStr)){ 
  31.   
  32.         $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
  33.         $fromUsername = $postObj->FromUserName; 
  34.         $toUsername = $postObj->ToUserName; 
  35.         $type = $postObj->MsgType; 
  36.         $customevent = $postObj->Event; 
  37.         $keyword = trim($postObj->Content); 
  38.         $time = time(); 
  39.         $textTpl = "<xml> 
  40.               <tousername><!--[CDATA[%s]]--></tousername> 
  41.               <fromusername><!--[CDATA[%s]]--></fromusername> 
  42.               <createtime>%s</createtime> 
  43.               <msgtype><!--[CDATA[%s]]--></msgtype> 
  44.               <content><!--[CDATA[%s]]--></content> 
  45.               <funcflag>0</funcflag> 
  46.               </xml>";        
  47.         if($type=="event" and $customrevent=="subscribe"){ 
  48.           $contentStr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书"; 
  49.           $msgType = "text"; 
  50.           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  51.           echo $resultStr; 
  52.           }   
  53.         if(!emptyempty( $keyword )) 
  54.         {                 
  55.           $msgType = "text"; 
  56.           if($keyword=="1"){ 
  57.           $contentStr = "qiphon";} 
  58.           if($keyword=="2"){ 
  59.           $contentStr = "test 。";} 
  60.           if($keyword=="3"){ 
  61.           $contentStr = "test333";}          
  62.           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  63.           echo $resultStr; 
  64.         }else{ 
  65.           echo "Input something..."; 
  66.         } 
  67.   
  68.     }else { 
  69.       echo ""; 
  70.       exit; 
  71.     } 
  72.   } 
  73.   
  74.   private function checkSignature() 
  75.   { 
  76.     $signature = $_GET["signature"]; 
  77.     $timestamp = $_GET["timestamp"]; 
  78.     $nonce = $_GET["nonce"];   
  79.   
  80.     $token = TOKEN; 
  81.     $tmpArr = array($token, $timestamp, $nonce); 
  82.     sort($tmpArr); 
  83.     $tmpStr = implode( $tmpArr ); 
  84.     $tmpStr = sha1( $tmpStr ); 
  85.   
  86.     if( $tmpStr == $signature ){ 
  87.       return true; 
  88.     }else{ 
  89.       return false; 
  90.     } 
  91.   } 
  92. } 
  93.   
  94. ?> 

 

Tags: php微信开发 php关注事件

分享到: