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


Posted in PHP onJune 13, 2018

本文实例为大家分享了php微信公众号获取位置信息的具体代码,供大家参考,具体内容如下

<?php
/**
 * wechat php test
 */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
 public function valid()
 {
  $echoStr = $_GET["echostr"];

  //valid signature , option
  if($this->checkSignature()){
   echo $echoStr;
   exit;
  }
 }

 public function responseMsg()
 {
  //get post data, May be due to the different environments
  $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

  //extract post data
  if (!empty($postStr)){

    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $type = $postObj->MsgType;
    $customevent = $postObj->Event;
    $latitude = $postObj->Location_X;
    $longitude = $postObj->Location_Y;
    $keyword = trim($postObj->Content);
    $time = time();
    $textTpl = "<xml>
       <ToUserName><![CDATA[%s]]></ToUserName>
       <FromUserName><![CDATA[%s]]></FromUserName>
       <CreateTime>%s</CreateTime>
       <MsgType><![CDATA[%s]]></MsgType>
       <Content><![CDATA[%s]]></Content>
       <FuncFlag>0</FuncFlag>
       </xml>";    
    if($type=="event" and $customevent=="subscribe"){
     $contentStr = "感谢你的关注\n回复1查看联系方式\n回复2查看最新资讯\n回复3查看法律文书";
     $msgType = "text";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     echo $resultStr;
     } 
    if($type=="image" ){
     $contentStr = "你的图片很棒!";
     $msgType = "text";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     echo $resultStr;
     } 
    if($type=="location" ){
     $contentStr = "你的纬度是{$latitude},经度是{$longitude},我已经锁定!";
     $msgType = "text";
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     echo $resultStr;
     }  
    if(!empty( $keyword ))
    {        
     $msgType = "text";
     if($keyword=="1"){
     $contentStr = "qiphon";}
     if($keyword=="2"){
     $contentStr = "test222";}
     if($keyword=="3"){
     $contentStr = "test333";}     
     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
     echo $resultStr;
    }else{
     echo "Input something...";
    }

  }else {
   echo "";
   exit;
  }
 }

 private function checkSignature()
 {
  $signature = $_GET["signature"];
  $timestamp = $_GET["timestamp"];
  $nonce = $_GET["nonce"]; 

  $token = TOKEN;
  $tmpArr = array($token, $timestamp, $nonce);
  sort($tmpArr);
  $tmpStr = implode( $tmpArr );
  $tmpStr = sha1( $tmpStr );

  if( $tmpStr == $signature ){
   return true;
  }else{
   return false;
  }
 }
}

?>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
PHP var_dump遍历对象属性的函数与应用代码
Jun 04 PHP
PHP中如何定义和使用常量
Feb 28 PHP
如何设置mysql允许外网访问
Jun 04 PHP
解析如何去掉CodeIgniter URL中的index.php
Jun 25 PHP
ThinkPHP自动转义存储富文本编辑器内容导致读取出错的解决方法
Aug 08 PHP
浅析php工厂模式
Nov 25 PHP
基于thinkPHP3.2实现微信接入及查询token值的方法
Apr 18 PHP
ThinkPHP5 验证器的具体使用
May 31 PHP
PHP中的Iterator迭代对象属性详解
Apr 12 PHP
PHP array_reverse() 函数原理及实例解析
Jul 14 PHP
PHP7新增函数
Mar 09 PHP
用php如何解决大文件分片上传问题
Jul 07 PHP
PHPExcel实现表格导出功能示例【带有多个工作sheet】
Jun 13 #PHP
ThinkPHP框架获取最后一次执行SQL语句及变量调试简单操作示例
Jun 13 #PHP
thinkphp5.1 文件引入路径问题及注意事项
Jun 13 #PHP
PHP基于DateTime类解决Unix时间戳与日期互转问题【针对1970年前及2038年后时间戳】
Jun 13 #PHP
php中html_entity_decode实现HTML实体转义
Jun 13 #PHP
php处理多图上传压缩代码功能
Jun 13 #PHP
Laravel框架使用Seeder实现自动填充数据功能
Jun 13 #PHP
You might like
php获取文件名后缀常用方法小结
2015/02/24 PHP
php中file_exists函数使用详解
2015/05/08 PHP
php实现表单提交上传文件功能
2018/05/28 PHP
jQuery 插件 将this下的div轮番显示
2009/04/09 Javascript
js获取电脑分辨率的思路及操作
2013/11/22 Javascript
jquery绑定事件不生效的解决方法
2014/02/11 Javascript
Node.js中使用计时器定时执行函数详解
2014/08/15 Javascript
z-blog SyntaxHighlighter 长代码无法换行解决办法(jquery)
2014/11/16 Javascript
完美实现仿QQ空间评论回复特效
2015/05/06 Javascript
JavaScript中的getTimezoneOffset()方法使用详解
2015/06/10 Javascript
bootstrap table复杂操作代码
2016/11/01 Javascript
JS表单验证方法实例小结【电话、身份证号、Email、中文、特殊字符、身份证号等】
2017/02/14 Javascript
Vue render渲染时间戳转时间,时间转时间戳及渲染进度条效果
2018/07/27 Javascript
基于游标的分页接口实现代码示例
2018/11/12 Javascript
微信小程序通过一个json实现分享朋友圈图片
2019/09/03 Javascript
layui清除radio的选中状态实例
2019/11/14 Javascript
[20:57]Ti4主赛事第三天开幕式
2014/07/21 DOTA
跟老齐学Python之print详解
2014/09/28 Python
Python实现的最近最少使用算法
2015/07/10 Python
Python之re操作方法(详解)
2017/06/14 Python
在windows下Python打印彩色字体的方法
2018/05/15 Python
Python语言检测模块langid和langdetect的使用实例
2019/02/19 Python
python交互模式下输入换行/输入多行命令的方法
2019/07/02 Python
sklearn的predict_proba使用说明
2020/06/28 Python
使用Python操作MySQL的小技巧
2020/09/10 Python
手把手教你配置JupyterLab 环境的实现
2021/02/02 Python
h5移动端调用支付宝、微信支付的实现
2020/06/08 HTML / CSS
台湾最大网路书店:博客来
2018/03/18 全球购物
什么是数组名
2012/05/10 面试题
介绍一下SQL Server的全文索引
2013/08/15 面试题
退休教师欢送会主持词
2014/03/31 职场文书
本溪水洞导游词
2015/02/11 职场文书
2015年社会治安综合治理工作总结
2015/04/10 职场文书
2016七夕情人节感言
2015/12/09 职场文书
pytorch显存一直变大的解决方案
2021/04/08 Python
mysql5.7使用binlog 恢复数据的方法
2021/06/03 MySQL