码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2016-03-07 06:42:38      阅读:370      评论:0      收藏:0      [点我收藏+]

标签:

这节课程我们来做一个游戏,这个游戏的名字叫“寻找美女做老婆”,

用户该游戏后,每隔5s(秒)会收到他距离未来老婆的信息,如果找到美女后,输入“老婆”即可成功。

 

这次我们涉及到一个车联网API,我们先来看下他在哪,张成什么样。

http://lbsyun.baidu.com/index.php?title=car

技术分享

 

找到接口说明,里面有一个测距,如下所示:

技术分享

 

 

 

我们可以看到这就是他的接口

 

技术分享

 

参数说明以及返回的参数如下所示:

 

技术分享

技术分享

 

很明显他就是返回一个XML或者是json格式的数据,默认返回的是一个XML格式的数据。

 

废话不多说,开工,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‘){
                            //如果存在close.txt文件则关闭游戏,嘻嘻
                            if(!file_exists(‘./close.txt‘)){
                                //先确定老婆的地理位置
                                $lp_Latitude = ‘21.518316‘; //纬度
                                $lp_Longitude = ‘111.005455‘; //经度
                                $lp_name = ‘彩虹‘; //老婆名称
                                
                                //接受用户上传的地理位置
                                $Latitude = $postObj->Latitude; //纬度
                                $Longitude = $postObj->Longitude; //经度
                                
                                //这里的ak也就是在应用列表里面找到的,和上一节课的一样
                                $ak = ‘LGkGwozOnoCxD9A4xZGHPqUs‘;
                                //现在我们根据这两个地理位置获取他们的距离
                                $query_url = "http://api.map.baidu.com/telematics/v3/distance?waypoints={$lp_Longitude},{$lp_Latitude};{$Longitude},{$Latitude}&ak={$ak}";
                                $query_res = file_get_contents($query_url);
                                
                                //使用simplexml对返回的结果进行解析
                                $query_res2 = simplexml_load_string($query_res, ‘SimpleXMLElement‘, LIBXML_NOCDATA);
                                //获取亮点的距离,
                                //result是因为他是存放在<result>这个节点里面的,distance指的是取出一条
                                $distance = $query_res2->results->distance;
                                //返回
                                if($distance <= 20 ){
                                    $contentStr = "恭喜你,已抱得美人归,请对{$lp_name}呐喊一声‘老婆我爱你’您即可获得免费豪华婚宴200桌";
                                    $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                                    echo $resultStr;
                                }else{
                                    $contentStr = "您当前的地理位置经度是:{$Longitude}\n\n
                                    纬度:{$Latitude}\n\n
                                    您距离未来老婆还有{$distance}米,要加油噢
                                    ";
                                    $resultStr = sprintf($tmp_arr[‘text‘], $fromUsername, $toUsername, $time, $contentStr);
                                    echo $resultStr;
                                }
                            }else{
                                
                            }
                        }
                        break;
                    case ‘text‘:
                        //回复文本模板
                        if(!empty($keyword)){
                            if($keyword == ‘老婆我爱你‘)
                            {
                                $contentStr = "老公,你好棒样,我们免费获得由夺命雷公狗集团赠送的200桌海鲜大餐囖,YES";
                                //创建一个文件
                                $handle = fopen(‘close.txt‘,‘w‘);
                                $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();

?>

 

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

标签:

原文地址:http://www.cnblogs.com/leigood/p/5249255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!