PHP脚本自动识别验证码查询汽车违章背景与原理
出于定期核查名下车辆是否存在交通违法记录的日常需求,开发了一套自动化辅助脚本,旨在简化并加速个人车辆的违章信息查询过程。
该方案的核心技术路径包括利用Cookie维持会话状态以模拟浏览器行为,以及通过OCR技术自动识别页面验证码,进而完成表单数据的自动提交。
系统依赖Tesseract-OCR这一开源光学字符识别引擎作为核心组件。
针对验证码识别环节,选用Tesseract-OCR技术,其官方源码仓库地址为:https://github.com/tesseract-ocr/tesseract
关于该OCR引擎的具体安装与配置指南,可参考官方文档链接:https://github.com/tesseract-ocr/tesseract
在PHP环境中,采用Tesseract-Ocr-For-PHP库来对接底层OCR能力。
该库的作用是对底层的OCR执行命令进行二次封装,提供更便捷的API调用方式。
相关PHP封装库的GitHub项目主页位于:https://github.com/thiagoalessio/tesseract-ocr-for-php
以下是具体的代码实现部分,目前版本尚未进行深度性能优化,仅供快速了解整体逻辑参考:
编程语言:PHP
<?php
require 'TesseractOCR.php';
function weizhang($car_code, $fdjh)
{
$shanghui = mb_substr($car_code, 0, 1, 'utf-8');
$pre = array(
'冀' => 'he',
'云' => 'yn'
);
$url_pre = $pre[$shanghui];
$headers = array(
'Host: '.$url_pre.'.122.gov.cn',
'Origin: http://'.$url_pre.'.122.gov.cn',
'Referer: http://'.$url_pre.'.122.gov.cn/views/inquiry.html?q=j',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36 QQBrowser/4.1.4132.400'
);
//初始化变量
$cookie_file = 'cookie.txt';
$login_url = "http://$url_pre.122.gov.cn/views/inquiry.html?q=j";
$post_url = "http://$url_pre.122.gov.cn/m/publicquery/vio";
$verify_code_url = "http://$url_pre.122.gov.cn/captcha?nocache=".time();
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);
$fp = fopen("verifyCode.jpg", "w");
fwrite($fp, $img);
fclose($fp);
$code = (new TesseractOCR('verifyCode.jpg'))->psm(7)->run();
$code = explode("\n", $code);
$code = $code[1];
echo $code.PHP_EOL;
if (strlen($code) != 4) {
return json_encode(array('code'=>500));
}
$data = array(
'hpzl'=>'02',
'hphm1b' => substr($car_code, -6),
'hphm' => $car_code,
'fdjh' => $fdjh,
'captcha' => $code,
'qm' => 'wf',
'page' => 1
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result = curl_exec($curl);
curl_close($curl);
//unlink($cookie_file);
//unlink('verifyCode.jpg');
return $result;
}
$count = 0;
// 车牌号
$car_code = '冀Dxxxxx';
// 发动机后6位
$fdjh = 'xxxxxx';
while (true) {
$count++;
if ($count>50) {
exit('查询失败');
}
$res = weizhang($car_code, $fdjh);
$info = json_decode($res, true);
echo $res.PHP_EOL;
if ($info['code'] == 200) {
echo '车牌号: '. $car_code.PHP_EOL;
echo '未处理违章数: '.$info['data']['content']['zs'];
exit();
}
}
程序运行效果展示

上述内容分享了利用PHP脚本结合验证码识别技术实现汽车违章自动查询的方法,希望能对读者有所帮助。若有任何疑问,欢迎留言交流,作者将及时回复。同时也感谢各位对本站的支持!