<?php
$cookie_file = "tmp.cookie";//做一個放cookie的文件
$login_url = "http://xxx.com/logon.php";//登錄網址
$verify_code_url = "http://xxx.com/verifyCode.php";
echo "正在獲取COOKIE...\n";
$curlj = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//獲取到的內容作為變量存儲,設置為1或者TRUE就不直接輸出
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);//鏈接時間
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file);//獲取COOKIE並存儲,把獲得到的cookie存儲為文件
$contents = curl_exec($curl);
curl_close($curl);
echo "COOKIE獲取完成,正在取驗證碼...\n";//取出驗證碼
$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);
$img = curl_exec($curl);
curl_close($curl);
$fp = fopen("verifyCode.jpg","w");
fwrite($fp,$img);
fclose($fp);
echo "驗證碼取出完成,正在休眠,20秒內請把驗證碼填入code.txt並保存\n";
//停止運行20秒
sleep(20);
echo "休眠完成,開始取驗證碼...\n";
$code = file_get_contents("code.txt");
echo "驗證碼成功取出:$code\n";
echo "正在准備模擬登錄...\n";
$post = "username=maben&pwd=hahahaha&verifycode=$code";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
//這一塊根據自己抓包獲取到的網站上的數據來做判斷
if(substr_count($result,"登錄成功")){
echo "登錄成功\n";
}
else{
echo "登錄失敗\n"; exit;
}