Create a captcha solver using 2captcha and PHP
Tags: 2captcha
Published: 2021-12-27 13:29:45

> 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](https://2captcha.com/).

### 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](https://2captcha.com/auth/register). After that, you can get your API key from [here](https://2captcha.com/enterpage).
![](https://user-images.githubusercontent.com/76736580/147381189-341c9f3f-3490-4c13-8ac6-666cf0799aee.png)

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](https://getcomposer.org/download/). 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.

```php
<!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:
![](https://user-images.githubusercontent.com/76736580/147381333-c9f6de61-9716-4bf7-8336-b2d26815584c.png)

Now let's code the PHP code.

```php
/**
 * 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:
![](https://user-images.githubusercontent.com/76736580/147381643-e0fc50a0-a374-47a3-8bf2-494f0821412b.PNG)

Nice! Now you can solve captchas with 2Captcha. See you soon!