步驟及原理:
1、在信息類(lèi)中新建一個(gè)變量,用來(lái)保存cookie。每次請(qǐng)求時(shí)判斷cookie是否為空。
2、如果cookie為空的話(huà),使用curl,設(shè)置好請(qǐng)求url,referer,useragent等參數(shù),向登陸頁(yè)面進(jìn)行請(qǐng)求,獲取到登陸頁(yè)面的cookie。
2、獲取到登陸頁(yè)面的cookie之后,設(shè)置好curl的各項(xiàng)參數(shù),一般最好附帶referer,表示發(fā)起請(qǐng)求的頁(yè)面。將post區(qū)域附帶好用戶(hù)名密碼及其他相關(guān)必要參數(shù),向登陸地址請(qǐng)求(一般post的數(shù)據(jù)不止是用戶(hù)名密碼,還附帶各種狀態(tài)),然后執(zhí)行curl。
3、服務(wù)器接收到登陸請(qǐng)求后,一般會(huì)設(shè)置session,返回的http的頭中,會(huì)包含在客戶(hù)端設(shè)置cookie的信息,用于保持登陸狀態(tài)。
4、http的返回頭中有一行set-cookie,可以用正則表達(dá)式將其抽離出來(lái),這個(gè)就是我們要的登陸狀態(tài)的cookie了。(為了方便匹配,可以先將http頭分離出來(lái),可以用curl_getinfo()函數(shù)獲取到頭的長(zhǎng)度,方便截取。)
5、可以用curl_getinfo()函數(shù)來(lái)獲取到狀態(tài)碼,來(lái)判斷登陸狀態(tài)。
6、下一次請(qǐng)求時(shí)攜帶獲取到的cookie,即可完成登陸,獲取到登陸界面的信息。
以下是登陸慕課網(wǎng)的示例:
$data='username=xxxxxxx@126.com&password=xxxxxxx&remember=1';
$curlobj = curl_init(); // 初始化
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // 設(shè)置訪問(wèn)網(wǎng)頁(yè)的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); // 執(zhí)行之后不直接打印出來(lái)
// Cookie相關(guān)設(shè)置,這部分設(shè)置需要在所有會(huì)話(huà)開(kāi)始之前設(shè)置
date_default_timezone_set('PRC'); // 使用Cookie時(shí),必須先設(shè)置時(shí)區(qū)
curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curlobj, CURLOPT_HEADER, 0);
// 注釋掉這行,因?yàn)檫@個(gè)設(shè)置必須關(guān)閉安全模式 以及關(guān)閉open_basedir,對(duì)服務(wù)器安全不利
//curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curlobj, CURLOPT_POST, 1);
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8",
"Content-length: ".strlen($data)
)); //設(shè)置http請(qǐng)求頭中的信息,以此讓服務(wù)器認(rèn)為是瀏覽器發(fā)出的請(qǐng)求
curl_exec($curlobj); // 執(zhí)行
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/space/index");
curl_setopt($curlobj, CURLOPT_POST, 0);
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type: text/xml"
));
$output=curl_redir_exec($curlobj); // 執(zhí)行
curl_close($curlobj); // 關(guān)閉cURL
echo $output;
/**
* 自定義實(shí)現(xiàn)頁(yè)面鏈接跳轉(zhuǎn)抓取
*/
function curl_redir_exec($ch,$debug="")
{
static $curl_loops = 0;
static $curl_max_loops = 20;
if ($curl_loops++ >= $curl_max_loops)
{
$curl_loops = 0;
return FALSE;
}
curl_setopt($ch, CURLOPT_HEADER, true); // 開(kāi)啟header才能夠抓取到重定向到的新URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
// 分割返回的內(nèi)容
$h_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($data,0,$h_len);
$data = substr($data,$h_len - 1);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = @parse_url(trim(array_pop($matches)));
// print_r($url);
if (!$url)
{
//couldn't process the url to redirect to
$curl_loops = 0;
return $data;
}
$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
if (!isset($url['scheme']))
$url['scheme'] = $last_url['scheme'];
if (!isset($url['host']))
$url['host'] = $last_url['host'];
if (!isset($url['path']))
$url['path'] = $last_url['path'];
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query'])?'?'.$url['query']:'');
curl_setopt($ch, CURLOPT_URL, $new_url);
return curl_redir_exec($ch);
} else {
$curl_loops=0;
return $data;
}