您的位置:首页 > 移动开发 > 微信开发

夺命雷公狗---微信开发46----获取用户地理位置接口(1)

2016-03-06 12:26 597 查看
我们先来看下手册有啥要求的先





我们先来写一个小功能,客户端每隔5s(秒)钟自动向公众平台报告自己的地理位置,服务器返回该地理位置给客户端。

我们要先到公众号管理界面里打开他才可以,因为默认的情况下他是关闭的,打开方法如下图所示:





如果开启成功后,他将会显示修改陈功,如下图所示:



我们在从新写过一个index.php的文件,代码如下所示:

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

//define your token
require_once "common.php";
//这里是引入curl发送函数的类
require_once 'WeChat.class.php';
define("TOKEN", "twgdh");

//这里让这个类继承了curl发送参数的类
class wechatCallbackapiTest extends WeChat
{
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
//extract post data
if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
// 使用simplexml技术对xml进行解析
// libxml_disable_entity_loader(true), 是从安全性考虑,为了防止xml外部注入,
//只对xml内部实体内容进行解析
libxml_disable_entity_loader(true);
//加载 postStr 字符串
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
global $tmp_arr;

//根据接收到的消息类型,来进行分支处理(switch)
switch($postObj->MsgType)
{
case 'event':
//用户未关注时,进行关注后的事件推送
if($postObj->Event == 'subscribe')
{

$contentStr = "夺命雷公狗欢迎您!, 您已经加入到 {$gid}";
$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
echo $resultStr;
}else if($postObj->Event == 'LOCATION')
{
//取出该用户上报的经度和纬度
$Latitude = $postObj->Latitude;//纬度
$Longitude = $postObj->Longitude;//经度
//现在是每隔5秒自动返回的
$contentStr = "您目前的地理位置是\n\n纬度是:{$Latitude}\n\n经度是:{$Longitude}";
$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
echo $resultStr;
}
break;
case 'text':
//回复文本模板
if(!empty($keyword))
{

$contentStr = "夺命雷公狗平台已经收到您的问题, 稍后回复您!";
$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
echo $resultStr;

}
else{
$contentStr = "您输入的格式不正确";
$resultStr = sprintf($tmp_arr['text'], $fromUsername, $toUsername, $time, $contentStr);
echo $resultStr;
}
break;
//default:
}
}else {
echo "";
exit;
}
}

private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}

$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

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

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

//如果这段代码放在上面,那程序将会报错,因为继承的问题,会显示类没有找到
$wechatObj = new wechatCallbackapiTest();
//当接入成功后,请注销这句话,否则,会反复验证。
//$wechatObj->valid();
//添加响应请求的语句
$wechatObj->responseMsg();

?>


核心代码如下:



那么我们的手机将会在5秒钟内自动返回自己的经度和纬度消息
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: