Share on facebook
Share on reddit
Share on twitter
Share on pinterest
Share on tumblr
Share on whatsapp

Create a captcha solver using 2captcha and PHP

Share on facebook
Share on twitter
Share on linkedin
Share on tumblr
Share on whatsapp
Share on pinterest

This is a sponsored post

If you want to write a sponsored post, email [email protected]

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.


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.


Now let’s create an index.php file in the root directory of your project.

<!DOCTYPE html>
<html lang="en">

    <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>
        .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;

    <div class="container">
        <h1>Solve Captchas with 2Captcha</h1>
        <p>Let's solve captchas with <a href="">2Captcha</a>.</p>

        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>


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
 * Include the 2captcha library
require "autoloader.php";
 * Check if the form is submitted
if (isset($_POST["a"])) {
     * Declare variables
    $captcha = $_POST["a"];
     * 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 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!

Subscribe on YouTube


Generate Smooth Shadows

Leave a Reply

Your email address will not be published. Required fields are marked *

Request a tutorial if you didn't find what you need.