搜索
首页后端开发php教程php中使用tesseract识别验证码,并且模拟登录,验证码错误

代码如下:

<code><?php header("Content-type:text/html;charset=utf-8");
/**
 * 模拟登录
 */

//1.初始化变量
$cookie_file = tempnam('./temp','cookie');
$login_url = "http://210.32.33.91:8080/reader/redr_verify.php";//登录页面
$verify_code_url = "http://210.32.33.91:8080/reader/captcha.php";//验证码页面

//2.获取cookies
echo "正在获取COOKIE...<br>";
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
//3.取出验证码
echo "COOKIE获取完成,正在取验证码...<br>";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);//保存cookie
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);//使用cookie
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);

$codename = time();
$fp = fopen("/home/wwwroot/default/tesseract/Test/images/$codename.png","w");
echo "<img  src="/static/imghwm/default1.png" data-src="/img/bVssL8" class="lazy" alt="php中使用tesseract识别验证码,并且模拟登录,验证码错误" >";
fwrite($fp,$img);
fclose($fp);
//开始识别验证码
echo "验证码取出完成,正在休眠,正在识别验证码...<br>";

passthru("/usr/bin/tesseract  /home/wwwroot/default/tesseract/Test/images/$codename.png /home/wwwroot/default/tesseract/Test/images/$codename");
$code = file_get_contents("./images/$codename.txt");

echo "验证码成功取出:$code<br>";

echo "正在准备模拟登录...<br>";

$post_url = "http://210.32.33.91:8080/reader/redr_verify.php";
//为安全性,此处密码不提供。
$post = "number=1111111&passwd=111111&captcha=$code&select=cert_no&returnUrl=";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
echo str_replace('captcha.php','http://210.32.33.91:8080/reader/captcha.php',$result);</code>

回复内容:

代码如下:

<code><?php header("Content-type:text/html;charset=utf-8");
/**
 * 模拟登录
 */

//1.初始化变量
$cookie_file = tempnam('./temp','cookie');
$login_url = "http://210.32.33.91:8080/reader/redr_verify.php";//登录页面
$verify_code_url = "http://210.32.33.91:8080/reader/captcha.php";//验证码页面

//2.获取cookies
echo "正在获取COOKIE...<br>";
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
//3.取出验证码
echo "COOKIE获取完成,正在取验证码...<br>";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);//保存cookie
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);//使用cookie
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);

$codename = time();
$fp = fopen("/home/wwwroot/default/tesseract/Test/images/$codename.png","w");
echo "<img  src="/static/imghwm/default1.png" data-src="/img/bVssL8" class="lazy" alt="php中使用tesseract识别验证码,并且模拟登录,验证码错误" >";
fwrite($fp,$img);
fclose($fp);
//开始识别验证码
echo "验证码取出完成,正在休眠,正在识别验证码...<br>";

passthru("/usr/bin/tesseract  /home/wwwroot/default/tesseract/Test/images/$codename.png /home/wwwroot/default/tesseract/Test/images/$codename");
$code = file_get_contents("./images/$codename.txt");

echo "验证码成功取出:$code<br>";

echo "正在准备模拟登录...<br>";

$post_url = "http://210.32.33.91:8080/reader/redr_verify.php";
//为安全性,此处密码不提供。
$post = "number=1111111&passwd=111111&captcha=$code&select=cert_no&returnUrl=";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
echo str_replace('captcha.php','http://210.32.33.91:8080/reader/captcha.php',$result);</code>

2016/1/25 14:51更新

Linux的话captcha目录,cookies目录给写权限
你一步步断点调试,看你那个程序生成的结果是多少,图片是多少,

php中使用tesseract识别验证码,并且模拟登录,验证码错误
代码在:https://github.com/rainwsy/sf/tree/master/library-OCR-login


更新:
你应该
1.将验证码存下来,跟文字结果对比下,
2.对比每次的session_id是否一致
3.CURLOPT_COOKIEJAR第一次用的时候存session_id就可以了,后面的操作用CURLOPT_COOKIEFILE来读取session_id,其实你可以对比下几次请求返回header头中的session_id是否一致
我的验证码识别结果:

php中使用tesseract识别验证码,并且模拟登录,验证码错误

写了个DEMO:

指出几个问题,获取session在获取验证码的那一步一并获取就OK,没必要先取得session再取验证码,
当看到账号密码隐藏的时候我再想这是给校友回答的吗?

php中使用tesseract识别验证码,并且模拟登录,验证码错误

<code><?php require_once 'OCR.php';
$loginUrl = "http://210.32.33.91:8080/reader/redr_verify.php"; // 登录页面
$captchaUrl = "http://210.32.33.91:8080/reader/captcha.php"; // 验证码页面

$cookie_file = __DIR__ . DIRECTORY_SEPARATOR . 'cookies' . DIRECTORY_SEPARATOR . date('YmdHis') . '.txt';

// 获取验证码
$captchaString = get($captchaUrl, $cookie_file, true);
$tempCaptchaFile = __DIR__ . DIRECTORY_SEPARATOR . 'captcha' . DIRECTORY_SEPARATOR . date('YmdHis') . '.gif';
file_put_contents($tempCaptchaFile, $captchaString);
/*既然你说不是验证码的问题,我就不发出来了*/
$ocr = new OCR($tempCaptchaFile);
$captcha = $ocr->getCaptcha();

/* 开始登陆 */
$username = '用户名';
$passwd = '密码';
$postArray = [
    'number' => $username,
    'passwd' => $passwd,
    'captcha' => $captcha,
    'select' => 'cert_no',
    'returnUrl' => ''
];
$postData = http_build_query($postArray);
echo post($loginUrl, $postData, $cookie_file);

function get($url, $cookie_file, $isCookiesSave = false)
{
    // 初始化
    $curl = curl_init($url);
    $header = array();
    $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    // 不输出header头信息
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if ($isCookiesSave) {
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); // 存储cookies
    } else {
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    }
    // 保存到字符串而不是输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 是否抓取跳转后的页面
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $info = curl_exec($curl);
    curl_close($curl);
    return $info;
}

function post($url, $data, $cookie_file)
{
    // 初始化
    $curl = curl_init($url);
    $header = array();
    $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    // 不输出header头信息
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 保存到字符串而不是输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    // post数据
    curl_setopt($curl, CURLOPT_POST, 1);
    // 请求数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    // 是否抓取跳转后的页面
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}</code>

网上的评论是 识别率低

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具