api地址:车首页
这个是免费api,可能是服务器不太稳定吧,经常请求不到数据,不过能用就行吧
坑之一:car_info拼接的时候需要在 首尾加上 {}
$info = '{hphm=' . $hphm
. '&classno=' . $classno
. '&engineno=' . $engineno
. '®istno=' . $registno
. '&city_id=' . $city_id
. '&car_type=' . $car_type
. '}';
$car_info = urlencode($info);
坑之二:时间戳为13位,而非常见的10位时间戳 可以使用$timestamp = time()*1000解决
static function free_api($member)
{
//车牌号码
$hphm = $member['plate1'].$member['plate2'].$member['plate_number'];
//完整车架号
$classno_all = $member['frame_number'];
//发动机号
$engineno_all = $member['engine_number'];
//证书编号
$registno_all = '';
/*****************根据车牌查询字段配置 Start***************/
if(1)
{
//发起请求
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"http://www.cheshouye.com/api/weizhang/get_all_config");
// CURLOPT_RETURNTRANSFER 设置是否有返回值
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
//执行完以后的返回值
$response = curl_exec($curl);
//释放curl
curl_close($curl);
//所有城市配置信息
$config = json_decode($response)->configs;
//该车牌城市配置
foreach ($config as $k=>$v)
{
if($v->province_short_name == substr($hphm, 0, 3))
{
foreach ($v->citys as $key=>$val)
{
if($val->car_head == substr($hphm, 0, 4))
{
$city_config = array('city_name' => $val->city_name,
'city_id' => $val->city_id,
'length_classno' => $val->classno,
'length_engineno' => $val->engineno,
'length_registno' => $val->registno);
}
}
}
}
}
/*****************根据车牌查询字段配置 End***************/
//根据城市配置得到classno engineno registno
if($city_config['length_classno'] == -1)
$classno = $classno_all;
elseif($city_config['length_classno'] == 0)
$classno = '';
else
$classno = substr($classno_all, (0-$city_config['length_classno']), $city_config['length_classno']);
if($city_config['length_engineno'] == -1)
$engineno = $engineno_all;
elseif($city_config['length_engineno'] == 0)
$engineno = '';
else
$engineno = substr($engineno_all, (0-$city_config['length_classno']), $city_config['length_classno']);
if($city_config['length_registno'] == -1)
$registno = $registno_all;
elseif($city_config['length_registno'] == 0)
$registno = '';
else
$registno = substr($registno_all, (0-$city_config['length_classno']), $city_config['length_classno']);
//违章查询地城市ID
$city_id = $city_config['city_id'];
//车辆车类型("02":小型汽车)
$car_type = 02;
$info = '{hphm=' . $hphm
. '&classno=' . $classno
. '&engineno=' . $engineno
. '®istno=' . $registno
. '&city_id=' . $city_id
. '&car_type=' . $car_type
. '}';
$car_info = urlencode($info);
$timestamp = time()*1000;
//自己申请的app_id
$app_id = '';
//自己申请的app_key
$app_key = '';
$sign = md5($app_id . $info . $timestamp . $app_key);
$url = 'http://www.cheshouye.com/api/weizhang/query_task'
. '?car_info=' . $car_info
. '×tamp=' . $timestamp
. '&app_id=' . $app_id
. '&sign=' . $sign;
//发起请求
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
// CURLOPT_RETURNTRANSFER 设置是否有返回值
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
//执行完以后的返回值
$response = curl_exec($curl);
//释放curl
curl_close($curl);
$result = json_decode($response);
switch ($result->status)
{
case 2001:
return json_encode(array(
'code' => 2001,
'msg' => '有违章记录',
'history' => $result->historys));
break;
case 2000:
return json_encode(array(
'code' => 2000,
'msg' => '无违章记录',
'history' => $result->historys));
break;
case 1002: //api有误
return json_encode(array(
'code' => 1002,
'msg' => '出错了',
'history' => []));
break;
case 1003: //sign加密有误
return json_encode(array(
'code' => 1003,
'msg' => '出错了',
'history' => []));
break;
case 1004: //车牌号,汽车类型,违章城市 等字段不能为空
return json_encode(array(
'code' => 1004,
'msg' => '车牌号,汽车类型,违章城市 等字段不能为空',
'history' => []));
break;
case 1005: //carInfo有误
return json_encode(array(
'code' => 1005,
'msg' => '出错了',
'history' => []));
break;
case 5000:
return json_encode(array(
'code' => 5000,
'msg' => '请求超时,请稍后重试',
'history' => []));
break;
case 5001:
return json_encode(array(
'code' => 5001,
'msg' => '交管局系统连线忙碌中,请稍后再试',
'history' => []));
break;
case 5002:
return json_encode(array(
'code' => 5002,
'msg' => '恭喜,当前城市交管局暂无您的违章记录',
'history' => []));
break;
case 5003:
return json_encode(array(
'code' => 5003,
'msg' => '数据异常,请重新查询',
'history' => []));
break;
case 5004:
return json_encode(array(
'code' => 5004,
'msg' => '系统错误,请稍后重试',
'history' => []));
break;
case 5005:
return json_encode(array(
'code' => 5005,
'msg' => '车辆查询数量超过限制',
'history' => []));
break;
case 5006:
return json_encode(array(
'code' => 5006,
'msg' => '你访问的速度过快, 请后再试',
'history' => []));
break;
case 5008:
return json_encode(array(
'code' => 5008,
'msg' => '输入的车辆信息有误,请查证后重新输入',
'history' => []));
break;
case -20:
return json_encode(array(
'code' => -20,
'msg' => '出错了',
'history' => []));
break;
default:
break;
}
}
笔记:
①如何在php中进行http请求
//发起请求
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
// CURLOPT_RETURNTRANSFER 设置是否有返回值
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
//执行完以后的返回值
$response = curl_exec($curl);
//释放curl
curl_close($curl);