php开发地图

来源:互联网 发布:unity3d 5.5粒子系统 编辑:程序博客网 时间:2024/06/07 15:27
<?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;
}
}
原创粉丝点击