-
header('content-type:text/html;charset=utf-8');
-
function curlPost($url,$data,$method){
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $url);
-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
-
-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
-
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
-
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
-
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
-
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
-
-
if($method=="POST"){
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
}
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
$tmpInfo = curl_exec($ch);
-
-
if (curl_errno($ch)) {
-
return curl_error($ch);
-
}
-
curl_close($ch);
-
return $tmpInfo;
-
}
-
$data=array('name' => '1234');
-
$url="http://www.sohu.com/";
-
-
$method="GET";
-
$file=curlPost($url,$data,$method);
-
$file=mb_convert_encoding($file,'UTF-8','GBK');
-
echo $file;
以下是模擬登陸的程式碼
-
<?php
-
$cookie_file = tempnam('./temp','cookie');
-
function weixinPost($url,$data,$method,$setcooke=false,$cookie_file=false){
-
$ch = curl_init();
-
curl_setopt($ch, CURLOPT_URL, $url);
-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
-
-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
-
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
-
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
-
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
-
-
if($method=="POST"){
-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
-
}
-
if($setcooke==true){
-
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
-
}else{
-
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
-
}
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
$tmpInfo = curl_exec($ch);
-
-
if (curl_errno($ch)) {
-
return curl_error($ch);
-
}
-
curl_close($ch);
-
return $tmpInfo;
-
}
-
$data=array('username' => '***','password'=>'***');
-
$url="http://www.xinxinj.com/login.php";
-
$method="POST";
-
$file=weixinPost($url,$data,$method,true,$cookie_file);
-
echo $file;
-
-
$url="http://www.xinxinj.com/admin.php";
-
$method="GET";
-
$file=weixinPost($url,$data,$method,false,$cookie_file);
-
echo $file;
-
-
?>
網上的很多模擬登入程式,大都是通過服務程式apache之類的執行,獲取到驗證碼之後顯示在網頁上,然後填上再POST出去
本文提供了一個程式例項,思路就是獲取到驗證碼之後把驗證碼儲存為一個圖片,然後程式休眠20秒,在20秒之後由使用者手動檢視圖片,並把驗證碼填寫到code.txt檔案中,20秒休眠完成後,程式會讀code.txt的驗證碼,這樣再帶著驗證碼進行登入操作。具體程式碼如下:
-
-
-
-
-
-
$cookie_file = "tmp.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);
-
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
-
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file);
-
$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";
-
-
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;
-
}
-
-