php通過curl擴充進行類比登入(含驗證碼)

來源:互聯網
上載者:User

標籤:工具   登入   time   lda   state   ipo   取出   自己   viewstate   

  以下為本人工作中遇到的需要做的事情,之前也沒怎麼用過curl,查了好多資料,才稍微弄明白一點;本文所有內容只是自己平日工作的記錄,僅供大家參考:

<?php
/**
* 類比登入
*/
header("Content-type: text/html; charset=UTF-8");
//初始設定變數
$cookie_file = "valid.tmp";
$login_url = "http://v3.gds.org.cn/login.aspx";
$verify_code_url = "http://v3.gds.org.cn/CheckCodeImg.aspx";

$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //擷取COOKIE並儲存
$contents = curl_exec($curl);
curl_close($curl);

//取出驗證碼
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);

//儲存驗證碼圖片
$fp = fopen("valid.jpg","wb");
fwrite($fp, $data);
fclose($fp);
//這裡取出驗證碼圖片後,程式休眠20s,這20s是用來手動查看驗證碼圖片,然後把驗證碼手動寫入目前的目錄下的code.txt檔案中,等待後續讀取
sleep(20);
//20s後開始讀取剛才手動寫入的驗證碼的TXT檔案獲得驗證碼
/*可下載驗證碼識別工具類,從而識別驗證碼,此處不再細說*/
$code = file_get_contents(‘code.txt‘);

//登入需要驗證的參數,根據登入的網站要求而定
$post = array("__VIEWSTATE"=>‘/wEPDwUKMjAxNzc1MTA0OA9kFgICAQ8WAh4GYWN0aW9uBQsvbG9naW4uYXNweBYEAgcPDxYCHghJbWFnZVVybAUgL0NoZWNrQ29kZUltZy5hc3B4P2lkPTIxMTk2ODAxMDNkZAIJDw9kFgIeB29uY2xpY2sFH0phdmFTY3JpcHQ6cmV0dXJuIExvZ2luQ2hlY2soKTtkZGMVVyyy1MfXv5mlHnNYOwIzMjm+sDJyRNMHE9LcjvgY‘,
"__VIEWSTATEGENERATOR"=>‘C2EE9ABB‘,
"__EVENTVALIDATION"=>‘/wEdAAUvG4CEDSAeffIfvT8/poMSt93hYdIOB6Ps/2Dq0TZfLrmXzh32huUyxvIXSZRQCCzJEpGhdun6s5y0mjTOD9huCal3PlhY9+LhlDAz50G/x28L/2U/T7kobvkKwEbiPO6upzznZuwcFVVFFeC+TWWb‘,
"UserID"=>‘1444743‘,
"Pwd"=>‘405769‘,
"KeyCode"=>$code,
"BTM"=>‘‘);


$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
?>

php通過curl擴充進行類比登入(含驗證碼)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.