PHP指令碼自動識別驗證碼查詢汽車違章詳解

來源:互聯網
上載者:User
經常有查下自己的車有沒有違章,所以寫了現在這個指令碼,協助查詢自己的車是否違章。主要用到,帶cookie類比表單提交和驗證碼識別。具體指令碼寫法一起通過本文學習吧

經常有查下自己的車有沒有違章,所以寫了現在這個指令碼,協助查詢自己的車是否違章。

主要用到,帶cookie類比表單提交和驗證碼識別。

Tesseract-OCR

驗證碼識別技術,Tesseract-OCR:https://github.com/tesseract-ocr/tesseract

安裝教程:https://github.com/tesseract-ocr/tesseract

Tesseract-Ocr-For-PHP

把需要執行的命令,封裝了一下

https://github.com/thiagoalessio/tesseract-ocr-for-php

直接上指令碼,沒進行什麼最佳化,簡單瞭解下:

PHP



<?phprequire '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();  }}


執行效果

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.