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

php微信开发之图片回复功能

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-14 15:26:46 浏览: 评论: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.         $customrevent = $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 = "33333333333"; 
  49.           $msgType = "text"; 
  50.           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  51.           echo $resultStr; 
  52.           }   
  53.         if($type=="image" ){ 
  54.           $contentStr = "你的图片很棒!"; 
  55.           $msgType = "text"; 
  56.           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  57.           echo $resultStr; 
  58.           }   
  59.   
  60.         if(!emptyempty( $keyword )) 
  61.         {                 
  62.           $msgType = "text"; 
  63.           if($keyword=="1"){ 
  64.           $contentStr = "333";} 
  65.           if($keyword=="2"){ 
  66.           $contentStr = "444 
  67.           if($keyword=="3"){ 
  68.           $contentStr = "11";}           
  69.           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  70.           echo $resultStr; 
  71.         }else{ 
  72.           echo "Input something..."; 
  73.         } 
  74.   
  75.     }else { 
  76.       echo ""; 
  77.       exit; 
  78.     } 
  79.   } 
  80.   
  81.   private function checkSignature() 
  82.   { 
  83.     $signature = $_GET["signature"]; 
  84.     $timestamp = $_GET["timestamp"]; 
  85.     $nonce = $_GET["nonce"];   
  86.   
  87.     $token = TOKEN; 
  88.     $tmpArr = array($token, $timestamp, $nonce); 
  89.     sort($tmpArr); 
  90.     $tmpStr = implode( $tmpArr ); 
  91.     $tmpStr = sha1( $tmpStr ); 
  92.   
  93.     if( $tmpStr == $signature ){ 
  94.       return true; 
  95.     }else{ 
  96.       return false; 
  97.     } 
  98.   } 
  99. } 
  100.   
  101. ?> 

 

Tags: php微信开发 php图片回复

分享到: