2Captcha is a human-powered image and CAPTCHA recognition service.
The process of solving captchas.
- We send the image to 2Captcha server.
- The server returns a unique task identifier (Captcha ID).
- Run a loop that checks whether the captcha is solved.
- As a result, we get either the text of the captcha or data for substitution in the sources of the page, or an error from the service.
Working with API 2Captcha in the program is simplified as much as possible. For this Human Emulator object has been added twocaptcha.
Let’s go through the main functions for working with the service 2Captcha.
Normal Captcha.
To recognize simple captcha with distorted text, use the function recognize.
As a result, the function returns a string with the solution of the captcha, which we substitute further in the input field on the site.
Using the function parameters you can set any type of such captcha, text or numbers, language, number of characters in the captcha, etc.
Parameters:
- filename – path to captcha file
- apikey – key for identification on the service
- path – the service path
- is_verbose – enable/disable verbose mode (commenting on what is happening): false( disable), true (enable)
- rtimeout – delay between polls of the status of the captcha
- mtimeout – timeout enter the captcha
- is_phrase – 0 or 1 – the flag “captcha contains two or more words”
- is_regsense – 0 or 1 – the flag “captcha is case sensitive”
- is_numeric – 0 – not specified; 1 – captcha contains only numbers; 2 – captcha contains only letters; 3 – captcha contains only numbers OR only letters; 4 – captcha contains both numbers AND letters
- min_len – 0 (no limit), minimal number of symbols in captcha
- max_len – 0 (no limit), maximal number of symbols in captcha
- is_russian – 0 – not specified; 1 – Cyrillic captcha; 2 – Latin captcha
Example of use in php:
1 2 3 4 5 6 7 8 9 10 11 |
// API key $twocaptcha->api_key="cdbecaf74aaf1aadafbd97189886ab92"; // 1 echo "1. Go to the polygon : "; echo $browser->navigate("http://www.humanemulator.net/poligon/image.html")."<br>"; // 2 echo "2. To recognize the captcha using the service 2captcha.com (as numbers): "; echo $image->screenshot_by_src("tmp/1.jpg","images/captcha",false)." "; echo $twocaptcha->recognize($debug->get_cur_script_folder()."tmp\\1.jpg",$twocaptcha->api_key,"http://www.2captcha.com",true,5,120,0,0,1)."\n"; |
As a result of the script, the text of the recognized captcha will appear in the debug panel of Human Emulator.
Text Captcha.
Text captcha is when you have to answer a question. For example: If tomorrow is Saturday, what day is today?
To recognize such captchas use the function recognize_text.
As a parameter, pass the text to be answered.
As a result, we get the correct answer, which must be either entered in a input field or selected from a listbox.
Example of using the function in php:
1 2 3 4 |
// API key $twocaptcha->api_key="cdbecaf74aaf1aadafbd97189886ab92"; // recognize text echo $twocaptcha->recognize_text("If tomorrow is Saturday, what day is today?")."<br>"; |
As a result of the example, the text with the correct answer will appear in the debug panel of Human Emulator.
ReCaptcha V2 and Invisible ReCaptcha V2.
Very popular captcha from Google. Different Invisible ReCaptcha V2 from the conventional ReCaptcha V2, that invisible captcha appears only after some action, say clicking on a button or field, and ReCaptcha V2 is visible on the page at once and looks like this:
There are two solutions to these captcha. First, getting a token and substitution of this token in the field on the site, and clicking on the specified pictures. Consider both solutions.
1.Recognition using a token.
This function is used recognize_recaptcha_v2.
This function takes the following parameters:
- pageurl – full URL of the page where you want to solve ReCaptcha V2
- googlekey – the so-called a sitekey. For each site it is different, and it can be found in the source text of the captcha.
- invisible – type of captcha. Default: 0. 1 – tells us that the site using invisible ReCaptcha V2.
- proxy – proxy. When you authenticate on IP: Ip_address:PORT. Example: proxy=123.123.123.123:3128. When you authenticate using login and password: loginparameters. Example: proxy=proxyuser:strongPassword@123.123.123.123:3128
- proxytype – the type of proxy: HTTP, HTTPS, SOCKS4, SOCKS5. Example: proxytype=SOCKS4
Proxy and proxytype parameters are not set for work without proxy.
After sending these parameters to the server 2captcha.com, in response comes the token this is the text like this:
1 |
03AHJ_Vuve5Asa4koK3KSMyUkCq0vUFCR5Im4CwB7PzO3dCxIo11i53epEraq-uBO5mVm2XRikL8iKOWr0aG50sCuej9bXx5qcviUGSm4iK4NC_Q88flavWhaTXSh0VxoihBwBjXxwXuJZ-WGN5Sy4dtUl2wbpMqAj8Zwup1vyCaQJWFvRjYGWJ_TQBKTXNB5CCOgncqLetmJ6B6Cos7qoQyaB8ZzBOTGf5KSP6e-K9niYs772f53Oof6aJeSUDNjiKG9gN3FTrdwKwdnAwEYX-F37sI_vLB1Zs8NQo0PObHYy0b0sf7WSLkzzcIgW9GR0FwcCCm1P8lB-50GQHPEBJUHNnhJyDzwRoRAkVzrf7UkV8wKCdTwrrWqiYDgbrzURfHc2ESsp020MicJTasSiXmNRgryt-gf50q5BMkiRH7osm4DoUgsjc_XyQiEmQmxl5sqZP7aKsaE-EM00x59XsPzD3m3YI6SRCFRUevSyumBd7KmXE8VuzIO9lgnnbka4-eZynZa6vbB9cO3QjLH0xSG3-egcplD1uLGh79wC34RF49Ui3eHwua4S9XHpH6YBe7gXzz6_mv-o-fxrOuphwfrtwvvi2FGfpTexWvxhqWICMFTTjFBCEGEgj7_IFWEKirXW2RTZCVF0Gid7EtIsoEeZkPbrcUISGmgtiJkJ_KojuKwImF0G0CsTlxYTOU2sPsd5o1JDt65wGniQR2IZufnPbbK76Yh_KI2DY4cUxMfcb2fAXcFMc9dcpHg6f9wBXhUtFYTu6pi5LhhGuhpkiGcv6vWYNxMrpWJW_pV7q8mPilwkAP-zw5MJxkgijl2wDMpM-UUQ_k37FVtf-ndbQAIPG7S469doZMmb5IZYgvcB4ojqCW3Vz6Q |
As a rule, we substitute the token in the field with the id “g-recaptcha-response“, but it depends on the site, there may be options with other fields or calling the so-called Callback functions.
See a detailed description of the nuances of the recognition of ReCaptcha V2 on 2captcha.com.
Example of using the function for both variants of ReCaptcha V2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
// API key $twocaptcha->api_key="cdbecaf74aaf1aadafbd97189886ab92"; // Example : ReCaptcha V2 echo "1. Go to demo webpage ReCaptcha V2: "; echo $browser->navigate("https://www.google.com/recaptcha/api2/demo")."<br>"; sleep(1); echo "2. Receiving the token from the recognition service: "; echo $result = $twocaptcha->recognize_recaptcha_v2($webpage->get_url(),"6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-"); echo "<br>"; echo "3. Use the received token to bypass the captcha: "; // set the token to a hidden field g-recaptcha-response $textarea->set_value_by_name("g-recaptcha-response", $result); // press the button Submit $button->click_by_id("recaptcha-demo-submit"); echo "<br>"; // pause 5 sec sleep(5); // Example : Invisible ReCaptcha V2 echo "1. Go to demo webpage Invisible ReCaptcha V2: "; echo $browser->navigate("https://www.google.com/recaptcha/api2/demo?invisible=true")."<br>"; sleep(1); echo "2. Get the current url of the page to send to the server : "; echo $url_loc = $webpage->get_url()."<br>"; echo "<br>"; echo "3. Receiving the token from the recognition service: "; echo $result = $twocaptcha->recognize_recaptcha_v2($webpage->get_url(),"6LfP0CITAAAAAHq9FOgCo7v_fb0-pmmH9VW3ziFs",1); echo "<br>"; echo "3. Use the received token to bypass the captcha: "; // set the token to a hidden field g-recaptcha-response $textarea->set_inner_html_by_name("g-recaptcha-response", $result); // call a callback function $browser->run_java_script('onSuccess();'); // press the button Submit $btn->click_by_id("recaptcha-demo-submit"); |
As a result of correct recognition of the captcha in the program will open a page with the text “Verification Success… Hooray!“.
2.Clicking on pictures to recognize ReCaptcha V2.
To solve ReCaptcha V2 this method uses the function recognize_click_captcha.
The function accepts the following parameters:
- filename – path to captcha image
- textinstructions – the text of what you want to select in the picture in UTF-8 encoding. This parameter is not required, if the picture is clear where to click – you can not send it.
Example of the recognition of ReCaptcha V2 with clicks on images in php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// API key $twocaptcha->api_key="cdbecaf74aaf1aadafbd97189886ab92"; // 1 echo "1. Go to demo webpage ReCaptcha V2 : "; echo $browser->navigate("https://www.google.com/recaptcha/api2/demo")."<br>"; // 2 echo("2. Click I'm not a robot : "); while (!$div->get_by_attribute("class","recaptcha-checkbox-checkmark",false,0)->is_visibled())sleep(1); echo $check=$div->get_by_attribute("class","recaptcha-checkbox-checkmark",false,0)->send_mouse_click(mt_rand(1,4),mt_rand(1,4))."\n"; // 3 echo "3. Save picture and question :"; while (!$image->get_by_src("www.google.com/recaptcha/api2/payload",false,1)->is_visibled())sleep(1); sleep(3); echo $image->screenshot_by_src("test\\tmp.jpeg","www.google.com/recaptcha/api2/payload",false,1)." "; // save text echo $describtion=explode("\n",$div->get_inner_text_by_attribute("class","imageselect-desc",false,1))[0]; // 4 // convert to UTF to send to the service $describtion=iconv("windows-1251","utf-8",$describtion); // recognize the captcha - as the one where you want to make clicks $solution=$twocaptcha->recognize_click_captcha("test\\tmp.jpeg",$describtion); // 5 echo "5. Click on the obtained coordinates : \n\n"; if (strpos($solution,"coordinates:")!=-1) { $solution=str_replace("coordinates:","",$solution); $solution=str_replace(";",",",$solution); $coords=explode(",",$solution); $image->get_by_src("test\\tmp.jpeg","www.google.com/recaptcha/api2/payload",false,1)->focus(); for ($i=0;$i<count($coords);$i+=2) { $x=str_replace("x=","",$coords[$i]) ; $y=str_replace("y=","",$coords[$i+1]); echo "click at (".$x." ".$y.") : на картинке капчи : "; echo $image->get_by_src("recaptcha/api2/payload",false,1)->send_mouse_move($x,$y)." "; usleep(50000); echo $image->get_by_src("recaptcha/api2/payload",false,1)->send_mouse_click($x,$y)."\n"; usleep(mt_rand(300000,1000000)); } echo "\n"; } $btn->click_by_id("recaptcha-verify-button", "1"); |
In the results of the example, the program clicks on the desired images. If the picture repeats, you need to send a new version, etc.
As you can see from the examples, the ReCaptcha V2 solution through a token is much easier than the option of clicking on the pictures, but in any case, both of these options work. The second method is suitable for all types of captcha, where you have to click on the pictures.
Recognition GeeTest captcha.
In GeeTest captcha, you need to select several objects or move a piece of the puzzle in the right order.
For raspoznanie this type of captcha in Human Emulator function is used recognize_geetest.
The following parameters must be passed for correct recognition:
- pageurl – Full URL of the page where you see GeeTest captcha
- gt – public website key (static)
- challenge – dynamic challenge key
- api_server – API domain (optional)
- proxy – proxy. When you authenticate on IP: Ip_address:PORT. Example: proxy=123.123.123.123:3128. When you authenticate using login and password: loginparameters. Example: proxy=proxyuser:strongPassword@123.123.123.123:3128
- proxytype – the type of proxy: HTTP, HTTPS, SOCKS4, SOCKS5. Example: proxytype=SOCKS4
Proxy and proxytype parameters are not set for work without proxy.
If captcha is already solved server will return the response in JSON. The response contains three values: challenge, validate and seccode:
1 2 3 |
"challenge":"1a2b3456cd67890e12345fab678901c2de", "validate":"09fe8d7c6ba54f32e1dcb0a9fedc8765", "seccode":"12fe3d4c56789ba01f2e345d6789c012|jordan" |
To solve the geetest captcha you need to substitute values in the appropriate fields greatest_challenge, gee test_validate, geetest_seccode and click on button with text “Click to verify”.
Example of recognition GeeTest captcha in the Human Emulator program in php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
// API key $twocaptcha->api_key="cdbecaf74aaf1aadafbd97189886ab92"; // 1 echo "1. Go to demo webpage contains geetest captcha: "; echo $browser->navigate("https://www.geetest.com/en")."<br>"; // let's go to the example of captcha $anchor->click_by_inner_text("Demo"); sleep(1); $li->set_focus_by_number(10); // 2 echo "2.To recognize the Geetest captcha using the service 2captcha.com : <br>"; // data to be sent to the site $api_server="api.geetest.com"; $date = new DateTime(); echo $t=$date->getTimestamp(); echo "<br>"; // get gt and challenge to submit to service echo $res = file_get_contents("https://www.geetest.com/demo/gt/register-enFullpage-official?t=$t"); echo "<br>"; // parse the data obtained from the answer $gt = get_string($res, '"gt":"', '",'); $challenge = get_string($res, '"challenge":"', '",'); echo "gt=$gt<br>challenge=$challenge<br>"; echo "<br>"; // send a service request $result = $twocaptcha->recognize_geetest("https://www.geetest.com/en", $gt, $challenge, $api_server); // parse the answer from the service $geetest_challenge=get_string($result, 'geetest_challenge":"', '",'); $geetest_validate=get_string($result, 'geetest_validate":"', '",'); $geetest_seccode=get_string($result, 'geetest_seccode":"', '",'); // полученный результат вводим в поля $hiddeninput->set_value_by_name("geetest_challenge", $geetest_challenge); $hiddeninput->set_value_by_name("geetest_validate", $geetest_validate); $hiddeninput->set_value_by_name("geetest_seccode", $geetest_seccode); // press the button Click to verify $span->click_by_inner_text("Click to verify"); echo "<hr><br>"; // parse a string in prefixes function get_string($str1, $pr1, $pr2, &$ind_st = 0) { // get the starting index $ind1 = strpos($str1, $pr1, $ind_st); if($ind1 === false) { return ""; } $ind1_1 = $ind1 + strlen($pr1); // get the finishing index $ind2 = strpos($str1, $pr2, $ind1_1); if ($ind2 === false) { return ""; } // result $sres = substr($str1, $ind1 + strlen($pr1), $ind2 - $ind1_1); return trim($sres); } |
As a result of the example, a green check mark appears on the button “Click to verify”.
We have reviewed the most commonly used types of captcha. All types of captchas that can be recognized through the service https://2captcha.com and tariffs can be found on the website of the service.
If you need to add recognition of some type of captcha, which is not in the program write to us on the our forum.