您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“PHP腳本如何自動識別驗證碼查詢汽車違章”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“PHP腳本如何自動識別驗證碼查詢汽車違章”這篇文章吧。
主要用到,帶cookie模擬表單提交和驗證碼識別。
Tesseract-OCR
驗證碼識別技術(shù),Tesseract-OCR:https://github.com/tesseract-ocr/tesseract
安裝教程:https://github.com/tesseract-ocr/tesseract
Tesseract-Ocr-For-PHP
把需要執(zhí)行的命令,封裝了一下
https://github.com/thiagoalessio/tesseract-ocr-for-php
直接上腳本,沒進行什么優(yōu)化,簡單了解下:
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'; // 發(fā)動機后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 '未處理違章數(shù): '.$info['data']['content']['zs']; exit(); } }
執(zhí)行效果
以上是“PHP腳本如何自動識別驗證碼查詢汽車違章”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。