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

【微信公众平台开发】百度周边搜索接口php封装

时间:2014-06-22 14:43:44      阅读:481      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   ext   

bubuko.com,布布扣

bubuko.com,布布扣

现在微信公众平台很多娱乐的,都有用到周边搜索功能,研究下比较简单,通过百度周边搜索接口封装如下:

调用格式:

$wechatBaiduAPI = new WechatBaiduAPI();
$ret = $wechatBaiduAPI->Place_search($str_key,$location['x'].",".$location['y'] );

参数说明:

$query:搜素关键词

$location: 地理位置经纬度

$radius: 搜索半径


<?php
	
	class WechatBaiduAPI{
		private $api_server_url;
		private $auth_params;
		
		public function __construct()
		{
			$this->api_server_url = "http://api.map.baidu.com/place/v2/";
			$this->auth_params = array();
			$this->auth_params['ak'] = "11ffd27d38deda622f51c9d314d46b1";
		}
		
		//http://api.map.baidu.com/place/search?&query=眼镜&location=39.915,116.404&radius=3000&output=json&key=37492c0ee6f924cb5e934fa08c6b1676
		public function Place_search($query, $location, $radius=3000)
		{
			return $this->call("search", array("output" =>json, "query" => $query,"page_size" =>10, "page_num" => 0,
					 "scope" => 2,"location" => $location, "radius" => $radius));
		}
		
		protected function call($method, $params = array())
		{
			$params = array_merge($this->auth_params, $params);
			$url = $this->api_server_url . "$method?".http_build_query($params);
			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			
			$data = curl_exec($ch);
			curl_close($ch);
			
			$result = null;
			
			$result = json_decode($data,true);
			
			return $result;
		}
		
	}

注意:代码里面的ak,是百度密钥,每个百度帐号都有一个唯一的ak,具体自己可以百度!
$this->auth_params['ak'] = "11ffd27d38deda622f51c9d314d46b1";

【微信公众平台开发】百度周边搜索接口php封装,布布扣,bubuko.com

【微信公众平台开发】百度周边搜索接口php封装

标签:style   class   blog   code   http   ext   

原文地址:http://blog.csdn.net/qivan/article/details/32717051

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