This is a sponsored post -- if you want to sponsor me email (posandumapa@#gmail.com)
Hello, devs! Today we will be creating a captcha solver using 2captcha. So before we start, let's see what is 2captcha and how it works.
What is 2captcha?
2captcha is software that allows you to solve captchas. It is a service that is used by many websites to solve captchas. You can see their website here.
How does it work?
First, we send an API request to 2captcha with the captcha and their workers solve it and we get the captcha solution.
Cost
The cost of using 2captcha is $0.77 per 1000 captcha. Pretty cheap.
Getting started
Make sure you have a 2captcha account. You can create one here. After that, you can get your API key from here.
And now that you have your API key, you can start using it.
Next, we need to install the 2captcha library. Make sure you have composer installed. If you don't, you can install it here. After that, you can install the library using the following command:
composer require 2captcha/2captcha
Once you have the library installed, you can start using it.
Coding
Now let's create an index.php
file in the root directory of your project.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Captcha Solver with 2Captcha</title>
<style>
.container {
max-width: 400px;
margin: 27px auto;
font-family: sans-serif;
}
b {
padding: 2px 18px;
background: #f0f0f0;
display: block;
border: 1px solid #c8c8c8;
color: #5e5e5e;
border-radius: 5px;
}
h1 {
font-size: 27px;
}
input[type="text"] {
margin-bottom: 10px;
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>Solve Captchas with 2Captcha</h1>
<p>Let's solve captchas with <a href="https://2captcha.com/">2Captcha</a>.</p>
<?php
if (isset($message)) {
echo "<b><p>$message</p></b>";
}
?>
<form method="POST">
<p>Enter text</p>
<input type="text" name="a" value="<?= (isset($_POST["a"]) ? $_POST["a"] : "") ?>">
<button type="submit">Submit</button>
</form>
</div>
</body>
</html>
This is the template of the index.php
file. It contains the form where you can enter the captcha and submit it. You can also see the $message
variable. This variable is used to display the result of the captcha. If the captcha is solved, the message will be displayed. If not, the message will be empty or the error message. Our result will be like this:
Now let's code the PHP code.
/**
* Set a time limit
*/
set_time_limit(130);
/**
* Include the 2captcha library
*/
require "autoloader.php";
/**
* Check if the form is submitted
*/
if (isset($_POST["a"])) {
/**
* Declare variables
*/
$captcha = $_POST["a"];
$captcha = preg_replace('/[^a-zA-Z0-9]/', '', $captcha);
/**
* Check the length of the captcha
*/
if (strlen($captcha) <= 2) {
$message = "The captcha is too short";
} else {
/**
* Create a new instance of the 2captcha class
* Don't forget to add your API key
*/
$solver = new \TwoCaptcha\TwoCaptcha('YOUR_API_KEY');
/**
* Send the captcha to the 2captcha.com API server and get the result
**/
try {
$result = $solver->text($captcha);
$message = "And the answer is, " . $result->code;
} catch (\Exception $e) {
$message = "Oops Something went wrong " . $e->getMessage();
}
}
}
Okay, now we have our code. Let's run it. Here's what we get:
Nice! Now you can solve captchas with 2Captcha. See you soon!
Top comments (2)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.