{ "status": "1", "info": "OK", "infocode": "10000", "results": [ "0": { "origin_id": "1", "dest_id": "1", "distance": "260296", "duration": "14340" }, "1": { "origin_id": "2", "dest_id": "1", "distance": "103905", "duration": "7500" }, "2": { "origin_id": "3", "dest_id": "1", "distance": "198549", "duration": "11520" } ] }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/7/20 17:50 */ //---------------------------------- // 距离测量 调用类 //---------------------------------- class freeApi{ private $apiUrl = 'https://restapi.amap.com/v3/distance?origins=116.481028,39.989643|114.481028,39.989643|115.481028,39.989643&destination=114.465302,40.004717&output=xml&key=<用户的key>'; /** * 获取 距离测量 结果 * @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 , 'free-api' ); 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; } }