Không cần SDK. Giải CAPTCHA trực tiếp từ thiết bị đầu cuối của bạn bằng cURL và API REST CaptchaAI. Hoàn hảo để thử nghiệm nhanh, đường dẫn CI/CD và tập lệnh shell.
Yêu cầu
| Yêu cầu | Chi tiết |
|---|---|
| cURL | Bất kỳ phiên bản hiện đại nào |
| jq (tùy chọn) | Để phân tích phản hồi |
| Khóa API CaptchaAI | Nhận một cái ở đây |
Các lệnh cơ bản
Kiểm tra số dư
curl -s "https://ocr.captchaai.com/res.php?key=YOUR_API_KEY&action=getbalance"
Đầu ra: 1.234
Gửi reCAPTCHA v2
curl -s "https://ocr.captchaai.com/in.php?key=YOUR_API_KEY&method=userrecaptcha&googlekey=6Le-wvkS...&pageurl=https://example.com"
Đầu ra: OK|73548291
Thăm dò kết quả
curl -s "https://ocr.captchaai.com/res.php?key=YOUR_API_KEY&action=get&id=73548291"
Đầu ra: OK|03AGdBq24PBCbw... hoặc CAPCHA_NOT_READY
Tập lệnh giải quyết Bash
Tạo solve_captcha.sh:
#!/bin/bash
set -euo pipefail
API_KEY="${CAPTCHAAI_API_KEY:?Set CAPTCHAAI_API_KEY environment variable}"
BASE_URL="https://ocr.captchaai.com"
solve_recaptcha() {
local site_key="$1"
local page_url="$2"
local timeout="${3:-300}"
# Submit
local response
response=$(curl -s "${BASE_URL}/in.php?key=${API_KEY}&method=userrecaptcha&googlekey=${site_key}&pageurl=${page_url}")
if [[ ! "$response" == OK|* ]]; then
echo "ERROR: Submit failed: $response" >&2
return 1
fi
local task_id="${response#OK|}"
echo "Submitted task: $task_id" >&2
# Poll
local deadline=$((SECONDS + timeout))
while (( SECONDS < deadline )); do
sleep 5
local result
result=$(curl -s "${BASE_URL}/res.php?key=${API_KEY}&action=get&id=${task_id}")
if [[ "$result" == "CAPCHA_NOT_READY" ]]; then
echo "Waiting..." >&2
continue
fi
if [[ "$result" == OK|* ]]; then
echo "${result#OK|}"
return 0
fi
echo "ERROR: Solve failed: $result" >&2
return 1
done
echo "ERROR: Timeout after ${timeout}s" >&2
return 1
}
# Usage: ./solve_captcha.sh SITE_KEY PAGE_URL
if [[ $# -ge 2 ]]; then
solve_recaptcha "$1" "$2"
fi
Làm cho nó có thể thực thi được:
chmod +x solve_captcha.sh
Chạy:
export CAPTCHAAI_API_KEY="your_key_here"
./solve_captcha.sh "6Le-wvkS..." "https://example.com"
Giải Cloudflare Turnstile
curl -s "https://ocr.captchaai.com/in.php?key=${CAPTCHAAI_API_KEY}&method=turnstile&sitekey=0x4AAAAA...&pageurl=https://example.com"
Giải CAPTCHA hình ảnh
# Encode image to base64
IMAGE_B64=$(base64 -w 0 captcha.png)
# Submit
curl -s "https://ocr.captchaai.com/in.php?key=${CAPTCHAAI_API_KEY}&method=base64&body=${IMAGE_B64}"
Đối với hình ảnh lớn, hãy sử dụng POST:
curl -s -X POST "https://ocr.captchaai.com/in.php" \
-F "key=${CAPTCHAAI_API_KEY}" \
-F "method=post" \
-F "file=@captcha.png"
Giải quyết và sử dụng mã thông báo trong một quy trình
#!/bin/bash
# Solve CAPTCHA and submit form in one pipeline
API_KEY="${CAPTCHAAI_API_KEY}"
SITE_KEY="6Le-wvkS..."
TARGET_URL="https://staging.example.com/qa-login"
# Solve
TOKEN=$(./solve_captcha.sh "$SITE_KEY" "$TARGET_URL")
if [[ -z "$TOKEN" ]]; then
echo "Failed to solve CAPTCHA"
exit 1
fi
# Submit form with token
curl -s -X POST "$TARGET_URL" \
-d "username=user" \
-d "password=pass" \
-d "g-recaptcha-response=${TOKEN}"
Xử lý hàng loạt
Giải nhiều CAPTCHA từ một tệp:
#!/bin/bash
# Input file: urls.txt (one URL per line)
while IFS= read -r url; do
echo "Processing: $url"
TOKEN=$(./solve_captcha.sh "6Le-wvkS..." "$url")
if [[ -n "$TOKEN" ]]; then
echo "$url,$TOKEN" >> results.csv
echo " Solved ✓"
else
echo " Failed ✗"
fi
done < urls.txt
PowerShell (Windows)
$ApiKey = $env:CAPTCHAAI_API_KEY
$BaseUrl = "https://ocr.captchaai.com"
# Submit
$response = Invoke-RestMethod "${BaseUrl}/in.php?key=${ApiKey}&method=userrecaptcha&googlekey=6Le-wvkS...&pageurl=https://example.com"
if ($response -match '^OK\|(.+)$') {
$taskId = $Matches[1]
Write-Host "Task: $taskId"
} else {
Write-Error "Submit failed: $response"
exit 1
}
# Poll
do {
Start-Sleep -Seconds 5
$result = Invoke-RestMethod "${BaseUrl}/res.php?key=${ApiKey}&action=get&id=${taskId}"
} while ($result -eq 'CAPCHA_NOT_READY')
if ($result -match '^OK\|(.+)$') {
$token = $Matches[1]
Write-Host "Token: $token"
} else {
Write-Error "Solve failed: $result"
}
Khắc phục sự cố
| Lỗi | nguyên nhân | sửa chữa |
|---|---|---|
curl: (6) Could not resolve host |
sự cố DNS | Kiểm tra mạng |
ERROR_WRONG_USER_KEY |
Khóa API không hợp lệ | Kiểm tra dấu cách/newlines trong khóa |
| Phản hồi trống | Hết thời gian chờ mạng | Thêm --connect-timeout 30 |
base64: invalid input |
Vấn đề về tệp nhị phân | Sử dụng base64 -w 0 (không gói) |
Câu hỏi thường gặp
Tôi có thể sử dụng tính năng này trong đường ống CI/CD không?
Vâng. Đặt CAPTCHAAI_API_KEY làm bí mật CI và gọi tập lệnh trong quy trình của bạn. Hoạt động với GitHub Actions, GitLab CI, Jenkins, v.v.
cURL có chậm hơn so với sử dụng SDK không?
Chi phí HTTP giống hệt nhau. cURL không có độ trễ so với các máy khách HTTP Python hoặc Node.js. Thời gian giải CAPTCHA chiếm ưu thế.
Làm cách nào để xử lý các ký tự đặc biệt trong URL?
Tham số mã hóa URL: sử dụng --data-urlencode với cURL POST hoặc curl -G --data-urlencode "pageurl=...".