php开发地图

<?php
namespace backend\controllers;


use Yii;
use yii\web\Controller;


/**

*/
class WeixinController extends controller
{
public $enableCsrfValidation=false;


public function actionInit()
{    
$db=yii::$app->db;
// echo $_GET['echostr'];die;
$postStr = file_get_contents("php://input");
file_put_contents("log/".time().rand(1000,9999).".log",$postStr);
$arr = (array)simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$type = $arr['MsgType'];
if ($type == 'event') {
if ($arr["Event"] == 'subscribe') {
$this->actionSendtext($arr,'欢迎关注');
}
}


if ($type == 'location') {
$sql = "insert into location(openid,location) values('".$arr['FromUserName']."','".$arr['Location_X'].",".$arr['Location_Y']."')";
$res = $db->createCommand($sql)->execute();
}


   if($type=="text") {
             $name=substr($arr['Content'],6);
     if($arr['Content']=="附近".$name)
     {
       $find=$db->createCommand("select * from location where openid='".$arr['FromUserName']."' limit 1")->queryOne();
       $str=$this->actionLocation($name,$find['location']);
       $this->actionSendtext($arr,$str);
     }
       }
}


public function actionSendtext($arr,$Content) 
{
$str="<xml>
      <ToUserName><![CDATA[".$arr['FromUserName']."]]></ToUserName>
      <FromUserName><![CDATA[".$arr['ToUserName']."]]></FromUserName>
      <CreateTime>".time()."</CreateTime>
      <MsgType><![CDATA[text]]></MsgType>
      <Content><![CDATA[".$Content."]]></Content>
      </xml>";
      echo $str;die;
}


public function actionLocation($q,$location)
{
$url = "http://api.map.baidu.com/place/v2/search";
$data = [
"query" => $q,
"output" => "json",
"scope" => 2,
"filter" => "sort_name:distance",
"ak" => "zn943QkxBhmLOhdfPDqzwOlgnEDBrIWt",
"radius" => 5000,
"location" => $location
];


$res = file_get_contents($url."?".http_build_query($data));
$res = json_decode($res,1);
// var_dump($res);die;
$str = '';
foreach ($res['results'] as $key => $value) {
$str.="名称:".$value['name']."--距离:".$value['detail_info']['distance']."米\r\n";
        }
        return $str;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值