首頁  >  文章  >  後端開發  >  PHP腳本自動辨識驗證碼查詢汽車違規詳解

PHP腳本自動辨識驗證碼查詢汽車違規詳解

墨辰丷
墨辰丷原創
2018-05-29 14:37:351180瀏覽

經常有查下自己的車有沒有違規,所以寫了現在這個腳本,幫忙查詢自己的車是否違規。主要用到,帶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



<?php
require &#39;TesseractOCR.php&#39;;
function weizhang($car_code, $fdjh)
{
  $shanghui = mb_substr($car_code, 0, 1, &#39;utf-8&#39;);
  $pre = array(
    &#39;冀&#39; => &#39;he&#39;,
    &#39;云&#39; => &#39;yn&#39;
  );
  $url_pre = $pre[$shanghui];
  $headers = array(
    &#39;Host: &#39;.$url_pre.&#39;.122.gov.cn&#39;,
    &#39;Origin: http://&#39;.$url_pre.&#39;.122.gov.cn&#39;,
    &#39;Referer: http://&#39;.$url_pre.&#39;.122.gov.cn/views/inquiry.html?q=j&#39;,
    &#39;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&#39;
  );
  //初始化变量
  $cookie_file = &#39;cookie.txt&#39;;
  $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(&#39;verifyCode.jpg&#39;))->psm(7)->run();
 $code = explode("\n", $code);
 $code = $code[1];
  echo $code.PHP_EOL;
  if (strlen($code) != 4) {
    return json_encode(array(&#39;code&#39;=>500));
  }
  $data = array(
    &#39;hpzl&#39;=>&#39;02&#39;,
    &#39;hphm1b&#39; => substr($car_code, -6),
    &#39;hphm&#39; => $car_code,
    &#39;fdjh&#39; => $fdjh,
    &#39;captcha&#39; => $code,
    &#39;qm&#39; => &#39;wf&#39;,
    &#39;page&#39; => 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(&#39;verifyCode.jpg&#39;);
  return $result;
}
$count = 0;
// 车牌号
$car_code = &#39;冀Dxxxxx&#39;;
// 发动机后6位
$fdjh = &#39;xxxxxx&#39;;
while (true) {
  $count++;
  if ($count>50) {
    exit(&#39;查询失败&#39;);
  }
  $res = weizhang($car_code, $fdjh);
  $info = json_decode($res, true);
  echo $res.PHP_EOL;
  if ($info[&#39;code&#39;] == 200) {
    echo &#39;车牌号: &#39;. $car_code.PHP_EOL;
    echo &#39;未处理违章数: &#39;.$info[&#39;data&#39;][&#39;content&#39;][&#39;zs&#39;];
    exit();
  }
}


#執行效果

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


相關推薦:

php中兩種守護程式的方式

php權限管理

css實作滑鼠觸發效果

以上是PHP腳本自動辨識驗證碼查詢汽車違規詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn