百度LBS云V3 云存储 云检索接口

<?php
/**
 * 百度 LBS云图数据接口
 * Author: IceCry <http://www.zhinizhiwo.com>
 * Date: 2017-10-14 15:51
 * 
 * 云存储使用V3 V4暂不支持云检索
 * 暂不使用sn签名
 * 百度LBS.云 http://lbsyun.baidu.com/index.php?title=lbscloud
 */
namespace ice;

class Bmap
{
    private static $ak;
    private static $tableid;

    public function __construct($ak='', $tableid=0) {
        if($ak===''){
            die('ak required !');
        }
        self::$ak = $ak;
        self::$tableid = $tableid;
    }
    /**
     * 创建位置数据表
     *
     * @param      <type>   $name          中文表名
     * @param      integer  $geotype       数据类型 1点 2线(暂不支持) 3面
     * @param      integer  $is_published  是否发布到云检索 1自动发布到检索
     */
    public function create_table($name, $geotype=1, $is_published=0, $sn='', $timestamp=0)
    {
        $url = 'http://api.map.baidu.com/geodata/v3/geotable/create';
        $data = [
            'ak' => self::$ak,
            'name' => $name,
            'geotype' => $geotype,
            'is_published' => $is_published
        ];

        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 查询表
     *
     * @param      string  $name   表名 可选
     * @param      string  $sn     签名
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function search_table($name='', $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/geotable/list';
        $data = [
            'ak' => self::$ak,
            'name' => $name
        ];

        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 查询指定id表
     *
     * @param      integer  $id     id
     * @param      string   $sn     sn
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function search_id($id=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/geotable/detail';
        $data = [
            'ak' => self::$ak,
            'id' => $id
        ];

        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 修改表
     *
     * @param      integer  $id            表id
     * @param      integer  $is_published  是否为云检索
     * @param      string   $name          表名
     * @param      string   $sn            The serial number
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function update_table($id=0, $is_published=0, $name='', $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/geotable/update';
        $data = [
            'ak' => self::$ak,
            'id' => $id,
            'name' => $name,
            'is_published' => $is_published,
            'sn' => $sn
        ];

        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 删除表 只能删除空表
     *
     * @param      integer  $id     表id
     * @param      string   $sn     The serial number
     */
    public function delete_table($id=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/geotable/delete';
        $data = [
            'ak' => self::$ak,
            'id' => $id,
            'sn' => $sn
        ];

        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 创建/更新列
     *
     * @param      string   $name                 中文列名
     * @param      string   $key                  列名
     * @param      integer  $type                 值类型 枚举值1:Int64, 2:double, 3:string, 4:在线图片url
     * @param      integer  $max_length           最大长度 1-2048 type为string有效 汉字数
     * @param      string   $default_value        默认值
     * @param      integer  $is_sortfilter_field  是否排序筛选
     * @param      integer  $is_search_field      是否文本检索
     * @param      integer  $is_index_field       是否存储引擎的索引字段
     * @param      integer  $is_unique_field      是否唯一索引
     * @param      string   $sn                   权限签名
     */
    public function deal_column($id=0, $name='', $key='', $type=0, $max_length=0, $default_value='', $is_sortfilter_field=0, $is_search_field=0, $is_index_field=0, $is_unique_field=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/column/create';
        $data = [
            'ak' => self::$ak,
            'name' => $name,
            'key' => $key,
            'type' => $type,
            'max_length' => $max_length,
            'default_value' => $default_value,
            'is_sortfilter_field' => $is_sortfilter_field,
            'is_search_field' => $is_search_field,
            'is_index_field' => $is_index_field,
            'is_unique_field' => $is_unique_field,
            'geotable_id' => self::$tableid,
            'sn' => $sn
        ];
        //如果存在id则更新
        if($id){
            $url = 'http://api.map.baidu.com/geodata/v3/column/update';
            $data['id'] = $id;
        }

        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 查询列
     *
     * @param      string  $name   表名 可选
     * @param      string  $key    属性key
     * @param      string  $sn     签名
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function search_column($name='', $key='', $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/column/list';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'name' => $name,
            'key' => $key,
            'sn' => $sn
        ];

        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 查询指定id列数据
     *
     * @param      integer  $id     The identifier
     * @param      string   $sn     The serial number
     */
    public function search_column_id($id=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/column/detail';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'id' => $id,
            'sn' => $sn
        ];

        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 删除指定列
     *
     * @param      integer  $id     列id
     * @param      string   $sn     签名
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function delete_column($id=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/column/delete';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'id' => $id,
            'sn' => $sn
        ];

        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 创建地理位置
     *
     * @param      string   $title       poi名称
     * @param      string   $address     地址
     * @param      string   $tags        标签
     * @param      <type>   $latitude    纬度
     * @param      <type>   $longitude   经度
     * @param      integer  $coord_type  坐标类型 1:GPS经纬度坐标 2:国测局加密经纬度坐标 3:百度加密经纬度坐标 4:百度加密墨卡托坐标
     * @param      string   $diy         自定义数据
     * @param      string   $sn          签名
     * @param      string   $diyvalue    自定义唯一索引key
     */
    public function deal_poi($id=0, $title='', $address='', $tags='', $latitude, $longitude, $coord_type=3, $diy=array(), $sn='', $diyvalue='')
    {
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'title' => $title,
            'address' => $address,
            'tags' => $tags,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'coord_type' => $coord_type
        ];
        $url = 'http://api.map.baidu.com/geodata/v3/poi/create';
        if($id){
            $url = 'http://api.map.baidu.com/geodata/v3/poi/update';
            $data['id'] = $id;
        }
        $data = array_merge($data, $diy);

        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 查询指定条件poi
     *
     * @param      array    $diy         自定义数据 is_index_field=1 string加$精准匹配
     * @param      string   $title       标题
     * @param      string   $tags        标签
     * @param      string   $bounds      矩形区域
     * @param      integer  $page_index  分页
     * @param      integer  $page_size   页面大小
     * @param      string   $sn          签名
     */
    public function search_poi_list($diy=[], $title='', $tags='', $bounds='', $page_index=0, $page_size=10, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/poi/list';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'title' => $title,
            'tags' => $tags,
            'bounds' => $bounds,
            'page_index' => $page_index,
            'page_size' => $page_size
        ];
        $data = array_merge($data, $diy);
        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 查询指定id的数据
     *
     * @param      integer  $id     id
     * @param      string   $sn     签名
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function search_id_poi($id=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/poi/detail';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'id' => $id
        ];
        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 删除poi 支持批量删除
     *
     * @param      integer  $id            删除单条 存在则其他条件无效
     * @param      string   $diyvalue      自定义唯一索引
     * @param      string   $ids           删除id列表1,2,3 批量删除尽可能使用此方法 <1000
     * @param      array    $diyIndex      用户自定义 需设置is_index_field=1
     * @param      string   $title         名称
     * @param      string   $tags          标签
     * @param      string   $bounds        矩形区域
     * @param      array    $diyColumn     自定义
     * @param      integer  $is_total_del  标记为批量删除 若仅设置为1不指定条件则删除全表
     * @param      string   $sn            签名
     */
    public function delete_poi($id=0, $diyvalue='', $ids='', $diyIndex=array(), $title='', $tags='', $bounds='', $diyColumn=array(), $is_total_del=0, $sn='')
    {
        $url = 'http://api.map.baidu.com/geodata/v3/poi/delete';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'id' => $id,
            'diyvalue' => $diyvalue,
            'ids' => $ids,
            'title' => $title,
            'tags' => $tags,
            'bounds' => $bounds,
            'is_total_del' => $is_total_del
        ];
        $arr = array_merge($diyIndex, $diyColumn);
        $data = array_merge($data, $arr);
        if(!$id){
            unset($data['id']);
        }
        $res = $this->https_request($url, $data);
        return $res;
    }

    /**
     * 批量上传数据 暂无用 每日限调用25次
     */
    public function create_multi_data()
    {
        $url = 'http://api.map.baidu.com/geodata/v3/poi/upload';
    }

    /**
     * 批量上传进度查询接口 暂无用
     */
    public function create_multi_progress()
    {
        $url = '';
    }

    /**
     * 批量操作查询结果 暂无用
     */
    public function multi_job_search()
    {

    }

    /**
     * 云检索 周边检索
     *
     * @param      string   $q           检索关键词
     * @param      string   $location    经纬度
     * @param      integer  $coord_type  坐标系 3百度经纬度
     * @param      integer  $radius      检索半径
     * @param      string   $tags        标签
     * @param      string   $sortby      排序字段
     * @param      string   $filter      过滤条件
     * @param      integer  $page_index  分页索引
     * @param      integer  $page_size   分页数量
     * @param      string   $callback    回调函数
     * @param      string   $sn          
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function search_nearby($q='', $location='', $coord_type=3, $radius=1000, $tags='', $sortby='', $filter='', $page_index=0, $page_size=10, $callback='', $sn='')
    {
        $url = 'http://api.map.baidu.com/geosearch/v3/nearby';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'q' => $q,
            'location' => $location,
            'coord_type' => $coord_type,
            'radius' => $radius,
            'tags' => $tags,
            'sortby' => $sortby,
            'filter' => $filter,
            'page_index' => $page_index,
            'page_size' => $page_size
        ];
        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 云检索 本地检索
     *
     * @param      string   $q           查询字符
     * @param      integer  $coord_type  坐标类型
     * @param      string   $region      检索区域 默认全国
     * @param      string   $tags        标签
     * @param      string   $sortby      排序
     * @param      string   $filter      过滤
     * @param      integer  $page_index  分页索引
     * @param      integer  $page_size   分页数量
     * @param      string   $callback    回调函数
     * @param      string   $sn          The serial number
     */
    public function search_local($q='', $coord_type=3, $region='', $tags='', $sortby='', $filter='', $page_index=0, $page_size=10, $callback='', $sn='')
    {
        $url = 'http://api.map.baidu.com/geosearch/v3/local';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'q' => $q,
            'coord_type' => $coord_type,
            'region' => $region,
            'tags' => $tags,
            'sortby' => $sortby,
            'filter' => $filter,
            'page_index' => $page_index,
            'page_size' => $page_size
        ];
        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 云检索 矩形检索
     *
     * @param      string   $q           查询内容
     * @param      string   $bounds      矩形区域
     * @param      string   $tags        The tags
     * @param      integer  $coord_type  The coordinate type
     * @param      string   $sortby      The sortby
     * @param      string   $filter      The filter
     * @param      integer  $page_index  The page index
     * @param      integer  $page_size   The page size
     * @param      string   $callback    The callback
     * @param      string   $sn          The serial number
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function search_bound($q='', $bounds='', $tags='', $coord_type=3, $sortby='', $filter='', $page_index=0, $page_size=10, $callback='', $sn='')
    {
        $url = 'http://api.map.baidu.com/geosearch/v3/bound';
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'q' => $q,
            'bounds' => $bounds,
            'tags' => $tags,
            'coord_type' => $coord_type,
            'sortby' => $sortby,
            'filter' => $filter,
            'page_index' => $page_index,
            'page_size' => $page_size
        ];
        $res = $this->https_request($url, $data, true);
        return $res;
    }

    /**
     * 云检索 poi详情检索
     *
     * @param      integer  $uid           poi点的id值
     * @param      integer  $coord_type  The coordinate type
     * @param      string   $sn          The serial number
     *
     * @return     <type>   ( description_of_the_return_value )
     */
    public function search_poi_detail($uid=0, $coord_type=3, $sn='')
    {
        $url = 'http://api.map.baidu.com/geosearch/v3/detail/'.$uid;
        $data = [
            'ak' => self::$ak,
            'geotable_id' => self::$tableid,
            'coord_type' => $coord_type
        ];
        $res = $this->https_request($url, $data, true);
        return $res;
    }


    /**
     * curl
     *
     * @param      <type>  $url    The url
     * @param      <type>  $data   The data
     * @param      <type>  $isget  是否为get请求
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function https_request($url, $data = null, $isget = false)
    {
        $headers = array(
            'Cache-Control:no-cache',
            'Pragma:no-cache'
        );

        if($isget){
            $url .= '?';
            if(is_array($data)){
                $url .= http_build_query($data);
            }elseif(is_string($data)) {
                $url .= $data;
            }
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_TIMEOUT,3);
        if (!empty($data) && !$isget){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}

Tags: 笔记

仅有一条评论

  1. 纠错

    部分方法参数类型错误

添加新评论