接口地址:http://api.map.baidu.com/traffic/v1/road |
---|
返回格式:json |
请求方式:get |
请求示例:http://api.map.baidu.com/traffic/v1/road?road_name=东二环&city=北京市&ak=你的AK |
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
ak | string | 必填 | 用户的AK,授权使用 扫码关注公众号 |
road_name | string | 必填 | 道路名称 |
city | string | 必填 | 所在城市 |
sn | string | 选填 | 若用户所用AK的校验方式为SN校验时该参数必须 |
名称 | 类型 | 说明 |
---|---|---|
status | int | 状态码 |
message | string | 响应信息 |
description | string | 路况语义化描述 |
evaluation | object | 路况整体评估 |
road_traffic | boject | 路况详细信息 |
{
"status": 0,
"message": "成功",
"description": "东二环:自北向南轻微拥堵;自南向北畅通;北向南,从东直门北桥到永安里桥拥堵;东向北,从东便门桥到五四运动火烧赵家楼遗址拥堵。",
"evaluation": {
"status": 1,
"status_desc": "自北向南轻微拥堵;自南向北畅通"
},
"road_traffic": [{
"congestion_sections": [{
"congestion_distance": 3160,
"speed": 18.84,
"status": 3,
"congestion_trend": "加重",
"section_desc": "北向南,从东直门北桥到永安里桥"
}, {
"congestion_distance": 1370,
"speed": 21.38,
"status": 3,
"congestion_trend": "缓解",
"section_desc": "东向北,从东便门桥到五四运动火烧赵家楼遗址"
}],
"road_name": "东二环"
}]
}
错误码 | 说明 |
---|---|
- | - |
<?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/traffic/v1/road?road_name=东二环&city=北京市&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;
}
}