PHP with Baidu map API for IP location and GPS positioning

Source: Internet
Author: User

<?php/** * Base coordinates to obtain the country, province, city, and the surrounding data class (using Baidu geocoding API implementation) * Baidu Key acquisition method: Http://lbsyun.baidu.com/apiconsole/key?  Application=key (need to register Baidu Developer account first) * date:2015-07-30 * author:fdipzone * ver:1.0 * * Func: * Public getaddresscomponent Get country, province, city and peripheral data by address * Private Tocurl using Curl call Baidu Geocoding API */class Geocoding {//Baidu Geocoding API Co    NST API = ' http://api.map.baidu.com/geocoder/v2/';    Do not display peripheral data const No_pois = 0;     Display peripheral Data Const POIS = 1;  /** * Base site for country, province, city and surrounding data * @param String $ak Baidu AK (Key) * @param Decimal $longitude Longitude * @param Decimal $latitude latitude * @param Int $pois Show Peripheral Data * @return Array */public static function GetAd                Dresscomponent ($ak, $longitude, $latitude, $pois =self::no_pois) {$param = array (' ak ' = = $ak,                ' Location ' =-implode (', ', Array ($latitude, $longitude)), ' pois ' = $pois,    ' Output ' = ' json ');    Request Baidu API $response = Self::tocurl (Self::api, $param);        $result = Array ();        if ($response) {$result = Json_decode ($response, true);    } return $result;  /** * Use Curl to invoke the Baidu geocoding API * @param String $url The requested address * @param Array $param The requested parameter * @return        JSON */private static function Tocurl ($url, $param =array ()) {$ch = Curl_init (); if (substr ($url, 0,5) = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer, false);//Skip certificate Check Curl_setop  T ($ch, Curlopt_ssl_verifyhost, true);        Check the existence of the SSL encryption algorithm from the certificate} curl_setopt ($ch, Curlopt_returntransfer, true);        curl_setopt ($ch, Curlopt_url, $url);        curl_setopt ($ch, Curlopt_post, true);        curl_setopt ($ch, Curlopt_postfields, Http_build_query ($param));        $response = curl_exec ($ch);        if ($error =curl_error ($ch)) {return false;        } curl_close ($ch);    return $response; }}?>

  

<?phpheader ("content-type:text/html; Charset=utf-8 "); header (' access-control-allow-origin:* '); Require "Geocoding.class.php"; $ak = ' 6q6ovs3llfuzcoxgtxvqxbyq ';//Baidu Key has changed to fill in its own class location{
private static $_instance; public $province; public $city; public $district; public $street; Public $address;
}function Test_input ($data) {$data = Trim ($data); $data = Stripslashes ($data); $data = Htmlspecialchars ($data); return $ data;} $longitude = Test_input ($_get["Long"), $latitude = Test_input ($_get["lat"]); $result = Geocoding::getaddresscomponent ($ak, $longitude, $latitude, Geocoding::no_pois); $locat =new location (); $address = $result ["Result"] [" Addresscomponent "];if ($address [" Province "]!= $address [" City "]) {$location = $address [" province "]. $address [" City "]. $address ["District"]. $address ["Street"];} else{$location = $address ["City"]. $address ["District"]. $address ["Street"];} $locat->province= $address ["Province"]; $locat->city= $address ["City"]; $locat->district= $address [" District "]; $locat->street= $address [" Street "]; $locat->address= $location; $json = Json_encode ($LOCAT); echo" $ JSON ";//Returns a similar address string and can return an address according to its own needs?>

Get the secret key address http://developer.baidu.com/map/

Reference link http://my.oschina.net/xialeistudio/blog/366347

PHP with Baidu map API for IP location and GPS positioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.