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

微信公众号开发之获取位置信息php代码

发布:smiling 来源: PHP粉丝网  添加日期:2018-10-11 14:23:43 浏览: 评论: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.     $latitude = $postObj->Location_X; 
  38.     $longitude = $postObj->Location_Y; 
  39.     $keyword = trim($postObj->Content); 
  40.     $time = time(); 
  41.     $textTpl = "<xml> 
  42.        <ToUserName><![CDATA[%s]]></ToUserName> 
  43.        <FromUserName><![CDATA[%s]]></FromUserName> 
  44.        <CreateTime>%s</CreateTime> 
  45.        <MsgType><![CDATA[%s]]></MsgType> 
  46.        <Content><![CDATA[%s]]></Content> 
  47.        <FuncFlag>0</FuncFlag> 
  48.        </xml>";     
  49.     if($type=="event" and $customevent=="subscribe"){ 
  50.      $contentStr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书"; 
  51.      $msgType = "text"; 
  52.      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  53.      echo $resultStr; 
  54.      }  
  55.     if($type=="image" ){ 
  56.      $contentStr = "你的图片很棒!"; 
  57.      $msgType = "text"; 
  58.      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  59.      echo $resultStr; 
  60.      }  
  61.     if($type=="location" ){ 
  62.      $contentStr = "你的纬度是{$latitude},经度是{$longitude},我已经锁定!"; 
  63.      $msgType = "text"; 
  64.      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  65.      echo $resultStr; 
  66.      }   
  67.     if(!emptyempty( $keyword )) 
  68.     {         
  69.      $msgType = "text"; 
  70.      if($keyword=="1"){ 
  71.      $contentStr = "qiphon";} 
  72.      if($keyword=="2"){ 
  73.      $contentStr = "test222";} 
  74.      if($keyword=="3"){ 
  75.      $contentStr = "test333";}      
  76.      $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  77.      echo $resultStr; 
  78.     }else{ 
  79.      echo "Input something..."; 
  80.     } 
  81.   
  82.   }else { 
  83.    echo ""; 
  84.    exit; 
  85.   } 
  86.  } 
  87.   
  88.  private function checkSignature() 
  89.  { 
  90.   $signature = $_GET["signature"]; 
  91.   $timestamp = $_GET["timestamp"]; 
  92.   $nonce = $_GET["nonce"];  
  93.   
  94.   $token = TOKEN; 
  95.   $tmpArr = array($token, $timestamp, $nonce); 
  96.   sort($tmpArr); 
  97.   $tmpStr = implode( $tmpArr ); 
  98.   $tmpStr = sha1( $tmpStr ); 
  99.   
  100.   if( $tmpStr == $signature ){ 
  101.    return true; //phpfensi.com 
  102.   }else{ 
  103.    return false; 
  104.   } 
  105.  } 
  106. } 
  107.   
  108. ?> 

Tags: 公众 位置 代码

分享到: