驗證碼,目前是找到URL,在前端網頁上直接顯示,然後手動輸入。
在網頁上用AJAX提交登陸資訊可以正確返回。
我將同樣的組合好的資訊,傳入後台PHP檔案中,用curl提交,但每次都報驗證碼錯誤。
感覺起來好像是在php中提交又重新整理了一次驗證碼,請問這個需要怎麼解決?
借用了ty0716的代碼試了一下,還是有一些問題,不明白要怎麼處理。
直接取驗證碼地址,沒有cookie儲存。用登陸頁面的地址,沒辦法取驗證碼來識別。
define('SCRIPT_ROOT',dirname(__FILE__).'/');include "code.php";function send($loginUrl, $cookieFile){ // $name = $_POST['name']; // $idcard = $_POST['idcard']; // $pw = $_POST['pw']; // $code = $_POST['code']; $code = getCode('http://www.xxx.com/Num.jsp'); header("Content-Type: text/html;charset=utf-8"); $post_data = "".$code; return post($loginUrl, $post_data, $cookieFile);}function getCode($captchaUrl, $cookieFile){ $captchaString = get($captchaUrl, $cookieFile); $tempCaptchaFile = SCRIPT_ROOT. date('YmdHis') . '.jpg'; file_put_contents($tempCaptchaFile, $captchaString); $ocr = new code($tempCaptchaFile); $captcha = $ocr->getCode(); echo $captcha; return $captcha;}function get($url, $cookie_file, $isCookiesSave = false){ // 初始化 $curl = curl_init($url); $header = array(); $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'; curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 不輸出header頭資訊 curl_setopt($curl, CURLOPT_HEADER, 0); if ($isCookiesSave) { curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); } else { curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file); } // 儲存到字串而不是輸出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 是否抓取跳轉後的頁面 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $info = curl_exec($curl); curl_close($curl); return $info;}function post($url, $data, $cookie_file){ // 初始化 $curl = curl_init($url); $header = array(); $header[0] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'; curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 不輸出header頭資訊 curl_setopt($curl, CURLOPT_HEADER, 0); // 儲存到字串而不是輸出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file); // post資料 curl_setopt($curl, CURLOPT_POST, 1); // 請求資料 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // 是否抓取跳轉後的頁面 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $response = curl_exec($curl); curl_close($curl); return $response;}
回複內容:
驗證碼,目前是找到URL,在前端網頁上直接顯示,然後手動輸入。
在網頁上用AJAX提交登陸資訊可以正確返回。
我將同樣的組合好的資訊,傳入後台PHP檔案中,用curl提交,但每次都報驗證碼錯誤。
感覺起來好像是在php中提交又重新整理了一次驗證碼,請問這個需要怎麼解決?
借用了ty0716的代碼試了一下,還是有一些問題,不明白要怎麼處理。
直接取驗證碼地址,沒有cookie儲存。用登陸頁面的地址,沒辦法取驗證碼來識別。
define('SCRIPT_ROOT',dirname(__FILE__).'/');include "code.php";function send($loginUrl, $cookieFile){ // $name = $_POST['name']; // $idcard = $_POST['idcard']; // $pw = $_POST['pw']; // $code = $_POST['code']; $code = getCode('http://www.xxx.com/Num.jsp'); header("Content-Type: text/html;charset=utf-8"); $post_data = "".$code; return post($loginUrl, $post_data, $cookieFile);}function getCode($captchaUrl, $cookieFile){ $captchaString = get($captchaUrl, $cookieFile); $tempCaptchaFile = SCRIPT_ROOT. date('YmdHis') . '.jpg'; file_put_contents($tempCaptchaFile, $captchaString); $ocr = new code($tempCaptchaFile); $captcha = $ocr->getCode(); echo $captcha; return $captcha;}function get($url, $cookie_file, $isCookiesSave = false){ // 初始化 $curl = curl_init($url); $header = array(); $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'; curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 不輸出header頭資訊 curl_setopt($curl, CURLOPT_HEADER, 0); if ($isCookiesSave) { curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); } else { curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file); } // 儲存到字串而不是輸出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 是否抓取跳轉後的頁面 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $info = curl_exec($curl); curl_close($curl); return $info;}function post($url, $data, $cookie_file){ // 初始化 $curl = curl_init($url); $header = array(); $header[0] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'; curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 不輸出header頭資訊 curl_setopt($curl, CURLOPT_HEADER, 0); // 儲存到字串而不是輸出 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file); // post資料 curl_setopt($curl, CURLOPT_POST, 1); // 請求資料 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // 是否抓取跳轉後的頁面 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $response = curl_exec($curl); curl_close($curl); return $response;}
你在第一次擷取頁面的時候,要儲存cookie,然後把你的登入資訊、驗證碼,連著那個cookie一起post過去。如果你直接post,你的驗證碼永遠對應的都是上一次的驗證碼
看了下,這樣的驗證碼可以做個驗證碼識別,參考我的一篇文章:https://segmentfault.com/a/1190000004361370
你需要保證提交post的session_id與驗證碼的session_id一致,否則就是驗證碼錯誤了。
驗證碼由你的程式來輸出,
用php擷取驗證碼後輸出,你再去調用一次要麼是到期的驗證碼,要麼就不是你這個session_id的驗證碼了
找到驗證碼的URL,在提交的時候請求一個驗證碼,聽過識別,或者是手動輸入