{ "status": 0, "message": "成功", "type": 2, "result": { "restriction": "", "total": 1, "routes": [{ "origin": { "lng": 116.339646, "lat": 40.010519 }, "destination": { "lng": 116.452389, "lat": 39.936406 }, "tag": "常规路线", "distance": 18616, "duration": 1699, "taxi_fee": 63, "toll": 0, "toll_distance": 0, "steps": [{ "leg_index": 0, "road_name": "无名路", "direction": 6, "distance": 541.64, "road_type": 6, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 0, "geo_cnt": 12, "distance": 541.64 }], "path": "116.339646,40.010519;116.339676,40.010169;116.339706,40.009359;116.339336,40.009351;116.339346,40.009271;116.339436,40.008411;116.339437,40.008291;116.338705,40.008266;116.338344,40.008268;116.338364,40.008018;116.338384,40.007878;116.338415,40.006998;116.338435,40.006748", "start_location": { "lng": 116.339646, "lat": 40.010519 }, "end_location": { "lng": 116.338435, "lat": 40.006748 } }, { "leg_index": 0, "road_name": "清华路", "direction": 9, "distance": 143.48, "road_type": 6, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 0, "geo_cnt": 3, "distance": 143.48 }], "path": "116.338435,40.006748;116.338355,40.006739;116.337392,40.006726;116.336750,40.006702", "start_location": { "lng": 116.338435, "lat": 40.006748 }, "end_location": { "lng": 116.33675, "lat": 40.006702 } }, { "leg_index": 0, "road_name": "东直门南大街", "direction": 6, "distance": 427.54, "road_type": 3, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 14, "distance": 427.54 }], "path": "116.440176,39.943837;116.440196,39.943417;116.440215,39.942927;116.440215,39.942817;116.440225,39.942636;116.440225,39.942576;116.440235,39.941986;116.440275,39.941655;116.440205,39.941627;116.440165,39.941547;116.440165,39.940997;116.440175,39.940797;116.440195,39.940427;116.440184,39.940197;116.440164,39.940037", "start_location": { "lng": 116.440176, "lat": 39.943837 }, "end_location": { "lng": 116.440164, "lat": 39.940037 } }, { "leg_index": 0, "road_name": "东四十条桥", "direction": 8, "distance": 222.65, "road_type": 3, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 15, "distance": 222.65 }], "path": "116.440164,39.940037;116.440134,39.940018;116.439893,39.939822;116.439872,39.939742;116.439883,39.939552;116.439983,39.939360;116.440214,39.939226;116.440255,39.939205;116.440345,39.939194;116.440386,39.939193;116.440879,39.939224;116.440919,39.939234;116.441040,39.939242;116.441160,39.939299;116.441341,39.939466;116.441362,39.939496", "start_location": { "lng": 116.440164, "lat": 39.940037 }, "end_location": { "lng": 116.441362, "lat": 39.939496 } }, { "leg_index": 0, "road_name": "工人体育场北路", "direction": 3, "distance": 630.89, "road_type": 4, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 11, "distance": 630.89 }], "path": "116.441362,39.939496;116.441663,39.939551;116.442036,39.939554;116.442186,39.939551;116.442277,39.939550;116.442508,39.939546;116.442629,39.939544;116.443263,39.939533;116.444309,39.939545;116.444953,39.939535;116.446380,39.939512;116.448783,39.939465", "start_location": { "lng": 116.441362, "lat": 39.939496 }, "end_location": { "lng": 116.448783, "lat": 39.939465 } }, { "leg_index": 0, "road_name": "无名路", "direction": 6, "distance": 7.79, "road_type": 6, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 1, "distance": 7.79 }], "path": "116.448783,39.939465;116.448783,39.939395", "start_location": { "lng": 116.448783, "lat": 39.939465 }, "end_location": { "lng": 116.448783, "lat": 39.939395 } }, { "leg_index": 0, "road_name": "工人体育场北路辅路", "direction": 3, "distance": 134.88, "road_type": 5, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 2, "distance": 134.88 }], "path": "116.448783,39.939395;116.449265,39.939378;116.450371,39.939362", "start_location": { "lng": 116.448783, "lat": 39.939395 }, "end_location": { "lng": 116.450371, "lat": 39.939362 } }, { "leg_index": 0, "road_name": "工人体育场西路", "direction": 6, "distance": 297.23, "road_type": 5, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 5, "distance": 297.23 }], "path": "116.450371,39.939362;116.450371,39.939182;116.450361,39.938783;116.450360,39.937963;116.450360,39.936773;116.450360,39.936693", "start_location": { "lng": 116.450371, "lat": 39.939362 }, "end_location": { "lng": 116.45036, "lat": 39.936693 } }, { "leg_index": 0, "road_name": "无名路", "direction": 3, "distance": 190.93, "road_type": 6, "toll": 0, "toll_distance": 0, "traffic_condition": [{ "status": 1, "geo_cnt": 1, "distance": 5.12 }, { "status": 0, "geo_cnt": 7, "distance": 185.81 }], "path": "116.450360,39.936693;116.450421,39.936692;116.450642,39.936659;116.450973,39.936634;116.451104,39.936633;116.451526,39.936617;116.451897,39.936592;116.452349,39.936597;116.452389,39.936406", "start_location": { "lng": 116.45036, "lat": 39.936693 }, "end_location": { "lng": 116.452389, "lat": 39.936406 } }] }] } }
<?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/direction/v2/driving?origin=40.01116,116.339303&destination=39.936404,116.452562&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; } }