Пример, выложенный на сайте самого сервиса – не компилируется minGW, поэтому я его пофиксил и немного доработал – результаты разгаданной капчи теперь не пролетают на экране, а сохраняются в файл.
Мои исправления:
— функция sleep(10) – должна быть записана с большой буквы и в микросекундах – Sleep(10000) – теперь скрипт будет спать 10 секунд.
— исправлен адрес сервиса, который везде был проставлен как anticaptcha.com – на ac-service.info
— добавлена функция file_put_contents(файл, содержимое) (о, пхп!). Функция сохраняет ответ антикапчи в указанный файл.
Вот код:
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
/* Anti-captcha.com C++ w/CURL usage example 1. replace variable "ackey" with your own 2. replace variable "captchafile" with your own captcha filename 3. compile with g++ option -lcurl */ #include #include //#include //#include <sys/stat.h> //#include #include #include #include <curl/curl.h> using namespace std; string contents,tempstr; string ackey="4ba8e36d76d571c4f37f786509834e19"; string captchafile="captcha.jpg"; size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream); bool myCheckIn(string& HTML, int *pos, string str, bool reg); int strpos(string& haystack, const char * needle); string substr(string& str, int pos, int offset); void file_put_contents(char *name, string res); int main() { int captcha_id=0; CURL *hCurl = NULL; CURLcode cc; char error_buffer[1024]; char tmp_char[256]; if (!hCurl) { cc = curl_global_init(CURL_GLOBAL_ALL); hCurl = curl_easy_init(); } if (hCurl) { char *url = "https://ac-service.info/in.php"; struct curl_httppost *post=NULL; struct curl_httppost *last=NULL; curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, handle_data); curl_easy_setopt(hCurl, CURLOPT_URL, url); curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, &error_buffer); curl_formadd(&post, &last, CURLFORM_COPYNAME, "method", CURLFORM_COPYCONTENTS, "post", CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, ackey.c_str(), CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME, "file", CURLFORM_FILE, captchafile.c_str(), CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END); //setting form fields to handler curl_easy_setopt(hCurl, CURLOPT_HTTPPOST, post); //posting curl_easy_perform(hCurl); //cleaning curl_formfree(post); curl_easy_cleanup(hCurl); curl_global_cleanup(); //checking for error if (strpos(contents,"ERROR")!=-1) { printf("found error: %sn",contents.c_str()); exit(1); } //getting id tempstr=substr(contents,3,10); captcha_id=atoi(tempstr.c_str()); printf("captcha id: %dn",captcha_id); contents=""; //creating new url sprintf(tmp_char,"https://ac-service.info/res.php?key=%s&action=get&id=%d",ackey.c_str(),captcha_id); //sleeping for 10-second periods and pinging anti-captcha while (1) { printf("sleeping for 10 seconds....n"); Sleep(10000); printf("checking for captcha result..."); hCurl = curl_easy_init(); if (hCurl) { curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, handle_data); curl_easy_setopt(hCurl, CURLOPT_URL, tmp_char); curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, &error_buffer); curl_easy_perform(hCurl); curl_easy_cleanup(hCurl); curl_global_cleanup(); if (strpos(contents,"NOT_READY")!=-1) { printf("captcha is not ready yetn"); } if (strpos(contents,"ERROR")!=-1) { //found error printf("error: %sn",contents.c_str()); exit(1); } if (strpos(contents,"OK")!=-1) { //captcha is ready tempstr=substr(contents,3,30); printf("captcha result: %sn",tempstr.c_str()); string res = "captcha result: "; res.append(tempstr.c_str()); file_put_contents("test.txt", res); break; } contents=""; } else { printf("CURL errorn"); exit(1); } } } else printf("CURL errorn"); printf("donen"); } size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream) { int numbytes = size*nmemb; char lastchar = *((char *) ptr + numbytes - 1); *((char *) ptr + numbytes - 1) = ''; contents.append((char *)ptr); contents.append(1,lastchar); *((char *) ptr + numbytes - 1) = lastchar; return size*nmemb; } bool myCheckIn(string& HTML, int *pos, string str, bool reg) { if(HTML.size() < *pos + str.size()) return false; for(unsigned int i = 0; i < str.size(); i++) { if(str[i] != HTML[i + *pos]) return false; } *pos += str.size() - 1; return true; } int strpos(string& haystack, const char * needle) { string tmp; tmp=needle; unsigned int hsize = haystack.size(); for (unsigned int i=0;i<hsize;i++) { if (myCheckIn(haystack, (int *) &i,tmp,true)) return (i-tmp.size()+1); } return -1; } string substr(string& str, int pos, int offset) { int i; string res; for (i=pos;i<(pos+offset);i++) { res += str[i]; } return res; } void file_put_contents(char *name, string res) { ofstream fout(name); fout << res; fout.close(); } |
Похожие записи: