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

php微信开发之音乐回复功能

发布:smiling 来源: PHP粉丝网  添加日期:2018-11-14 15:26:02 浏览: 评论: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.     $keyword = trim($postObj->Content); 
  36.     $time = time(); 
  37.     $textTpl = "<xml> 
  38.        <tousername><!--[CDATA[%s]]--></tousername> 
  39.        <fromusername><!--[CDATA[%s]]--></fromusername> 
  40.        <createtime>%s</createtime> 
  41.        <msgtype><!--[CDATA[%s]]--></msgtype> 
  42.        <content><!--[CDATA[%s]]--></content> 
  43.        <funcflag>0</funcflag> 
  44.        </xml>";     
  45.     if(!emptyempty( $keyword )) 
  46.     { 
  47.      $msgType = "text"; 
  48.      if($keyword=="1"){ 
  49.      $contentStr = "qiphon";} 
  50.      if($keyword=="2"){ 
  51.      $contentStr = "最新资讯:《环境违法行为举报奖励办法》";} 
  52.      if($keyword=="3"){ 
  53.      $contentStr = "31离婚协议\n32租房合同\n33交通事故协议书";}      
  54.      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  55.      echo $resultStr; 
  56.     }else{ 
  57.      echo "Input something..."; 
  58.     } 
  59.   
  60.   }else { 
  61.    echo ""; 
  62.    exit; 
  63.   } 
  64.  } 
  65.   
  66.  private function checkSignature() 
  67.  { 
  68.   $signature = $_GET["signature"]; 
  69.   $timestamp = $_GET["timestamp"]; 
  70.   $nonce = $_GET["nonce"];  
  71.   
  72.   $token = TOKEN; 
  73.   $tmpArr = array($token, $timestamp, $nonce); 
  74.   sort($tmpArr); 
  75.   $tmpStr = implode( $tmpArr ); 
  76.   $tmpStr = sha1( $tmpStr ); 
  77.   //phpfensi.com 
  78.   if( $tmpStr == $signature ){ 
  79.    return true; 
  80.   }else{ 
  81.    return false; 
  82.   } 
  83.  } 
  84. } 
  85.   
  86. ?> 

 

Tags: php微信开发 php音乐回复

分享到: