研究curl post登陸php
一步一步來,一個星期前我也不知道curl是啥。都是百度學習的,好了貼代碼html
這個是沒有驗證碼的登陸(帳號密碼本身申請一個)android
<?php
$cookieSuccess = dirname(__FILE__)."/1769.tmp";
$ch = curl_init();
// 用戶名\密碼
$user = "ha王";
$pass = "asd1asd123";
$questionid=array('value'=>0);
$url = "http://www.mala.cn/member.php?mod=logging&action=login";
// 返回結果存放在變量中,不輸出
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieVerify);
//curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
$fields_post = array("username"=> $user, "password"=> $pass,'value'=>0,"loginsubmit" => "Submit");
$headers_login = array("User-Agent" => "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36");
$fields_string = "";
foreach($fields_post as $key => $value){
$fields_string .= $key . "=" . $value . "&";
}
$fields_string = rtrim($fields_string , "&");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);segmentfault
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieSuccess);//用來存放登陸成功的cookiecookie
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result= curl_exec($ch);
curl_close($ch);
echo $result;curl
下面是cookie模擬登陸post
------------------------------------------------------------------------------------------------學習
$url = "http://www.mala.cn/thread-14378684-1-1.html";
$cookieSuccess = dirname(__FILE__)."/1769.tmp";
function request_url_data($url)
{
global $cookieSuccess;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieSuccess); //使用上面獲取的cookies
$data=curl_exec($ch);
curl_close($ch);
return $data;
}ui
echo request_url_data($url);url
--------------------------------------------------------------------------------------------------------------
cuir更多資料:http://www.android100.org/html/201406/28/32637.html
http://www.tuicool.com/articles/3u2MNf
https://segmentfault.com/q/1010000006103392