{ "status": "1", "info": "OK", "infocode": "10000", "count": "1", "geocodes": [{ "formatted_address": "北京市朝阳区阜通东大街|6号", "country": "我们", "province": "北京市", "citycode": "010", "city": "北京市", "district": "朝阳区", "township": [], "neighborhood": { "name": [], "type": [] }, "building": { "name": [], "type": [] }, "adcode": "110105", "street": "阜通东大街", "number": "6号", "location": "116.483038,39.990633", "level": "门牌号" }] }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/7/20 17:50 */ //---------------------------------- // 地理编码 调用类 //---------------------------------- class freeApi{ private $apiUrl = 'https://restapi.amap.com/v3/geocode/geo?key=youkey&address=北京市朝阳区阜通东大街6号'; /** * 获取 地理编码 结果 * @return array */ public function getResult(){ return $this->freeApiCurl($this->apiUrl); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false,$ispost=0){ $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , 'chuanshuoapi' ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 60); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); if( $ispost ) { curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); } else { if($params){ curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { return false; } curl_close( $ch ); return $response; } }