申請 AppKey:聚合數據違章查詢
header('Content-type: text/html; charset=utf-8');
include 'class.juhe.wz.php';
$appkey = 'YOUR_APPKEY';
$wz = new wz($appkey);
// 1. 獲取支持城市(建議緩存)
$citys = $wz->getCitys(); // 全部
$citys = $wz->getCitys('GD'); // 指定省份
// 2. 查詢違章
$city = 'GD_DG'; // 城市代碼
$carno = '粵S*****'; // 車牌號
$engineno = '****'; // 發動機號(部分城市必填)
$classno = '*****'; // 車架號(部分城市必填)
$result = $wz->query($city, $carno, $engineno, $classno);
if ($result['error_code'] == 0) {
if ($result['result']['lists']) {
foreach ($result['result']['lists'] as $item) {
echo $item['area'] . " " . $item['date'] . " " . $item['act'] . " " . $item['fen'] . " " . $item['money'] . "<br>";
}
} else { echo "無違章記錄"; }
} else { echo $result['error_code'] . ":" . $result['reason']; }
class wz {
private $appkey, $cityUrl='https://v.juhe.cn/wz/citys', $wzUrl='https://v.juhe.cn/wz/query';
public function __construct($appkey) { $this->appkey = $appkey; }
public function getCitys($province=false) {
$params = 'key='.$this->appkey.'&format=2'.($province?'&province='.$province:'');
return $this->_returnArray($this->juhecurl($this->cityUrl, $params));
}
public function query($city, $carno, $engineno='', $classno='') {
$params = array('key'=>$this->appkey, 'city'=>$city, 'hphm'=>$carno,
'engineno'=>$engineno, 'classno'=>$classno);
return $this->_returnArray($this->juhecurl($this->wzUrl, $params, 1));
}
private function juhecurl($url, $params, $ispost=0) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ispost?$url:$url.'?'.$params);
if ($ispost) { curl_setopt($ch, CURLOPT_POSTFIELDS, $params); }
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
private function _returnArray($json) { return json_decode($json, true); }
}