{ "status": 0, "result": [{ "distance": { "text": "18.8公里", "value": 18827 }, "duration": { "text": "12分钟", "value": 705 } }, { "distance": { "text": "20.6公里", "value": 20615 }, "duration": { "text": "13分钟", "value": 773 } }, { "distance": { "text": "44.7公里", "value": 44742 }, "duration": { "text": "28分钟", "value": 1677 } }, { "distance": { "text": "46.5公里", "value": 46530 }, "duration": { "text": "29分钟", "value": 1744 } }], "message": "成功" }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2019/9/3 23:10 */ //---------------------------------- // 批量算路 调用类 //---------------------------------- class freeApi{ private $ak; private $apiUrl; public function __construct($ak){ $this->ak = $ak; $this->apiUrl = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=40.45,116.34|40.54,116.35&destinations=40.34,116.45|40.35,116.46&ak='.$this->ak; } /** * 获取结果 * @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; } }