Rumah >pembangunan bahagian belakang >tutorial php >PHP脚本自动识别验证码查询汽车违章详解

PHP脚本自动识别验证码查询汽车违章详解

墨辰丷
墨辰丷asal
2018-05-29 14:37:351185semak imbas

经常有查下自己的车有没有违章,所以写了现在这个脚本,帮助查询自己的车是否违章。主要用到,带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实现鼠标触发效果

Atas ialah kandungan terperinci PHP脚本自动识别验证码查询汽车违章详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn