PHP脚本自动识别验证码查询汽车违章
程序员文章站
2024-04-01 19:03:34
经常有查下自己的车有没有违章,所以写了现在这个脚本,帮助查询自己的车是否违章。
主要用到,带cookie模拟表单提交和验证码识别。
tesseract-ocr
验证码...
经常有查下自己的车有没有违章,所以写了现在这个脚本,帮助查询自己的车是否违章。
主要用到,带cookie模拟表单提交和验证码识别。
tesseract-ocr
验证码识别技术,tesseract-ocr:
安装教程:
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脚本自动识别验证码查询汽车违章,希望对大家有所帮助