curl提交登陸資訊遇到驗證碼問題

來源:互聯網
上載者:User
關鍵字 php
驗證碼,目前是找到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,在提交的時候請求一個驗證碼,聽過識別,或者是手動輸入

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.