-
- $data='username=zjzhoufy@126.com&password=1q2w3e&remember=1';curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // 設定造訪網頁的URL
- curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true) ; // 執行後不直接列印出來
-
- // Cookie相關設置,這部分設定需要在所有會話開始前設定
- date_default_timezone_set('PRC'); // 使用Cookie時,必須先設定時區
- curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE);
- curl_setopt($curlobj, CURLOPT_COOKIEJAR, 'cookie。 ; //讀取
- curl_setopt($curlobj, CURLOPT_HEADER, 0);
- curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); // 這樣能夠讓cURL支援頁鏈結跳到
- curl_setopt($curlobj, CURLOPT_POST, 1);
- curl_setopt($curlobj, CURDS_DSTp); $curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8",
- "Content-length: ".strlen($data)
- ));
- curl_exec ($curlobj); // 執行
- 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_exec($curlobj); // 執行
- $output=curl_exec($curlobj); // 執行
- curl_close(curlbjobjob / 關閉cURL
- echo $output;
- ?>
-
-
- 複製程式碼
-
如上程式碼並不完美,我們應該把cookie存到快取中,不應該持久化,並且這樣的持久化只能是單一使用者存取才行。
所以,其實我們只要設置
CURLOPT_COOKIESESSION
複製代碼-
如下即可
- $data='username=demo_demo@126.com&password=123456qwe&remember=1'demo@126.com&password=123456qqwe&remember=1'demo@126.com&passwordqwe&rem>; $curlobj = curl_init(); // 初始化
- curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login"); // 設定造訪網頁的URL
- curl_setopt($ curlobj, CURLOPT_RETURNTRANSFER, true); // 執行之後不直接列印出來
-
- // Cookie相關設置,這部分設定需要在所有會話開始之前設定
- date_default_timezone_set('PRC'); // 使用Cookie時,必須先設定時區
- curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE);
- curl_setopt($curlobj, CURLOPT_HEADER, 0);
- curl_urls, fUR. .session_id());
- // 註解掉這行,因為這個設定必須關閉安全模式以及關閉open_basedir,對伺服器安全不利
- //curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1);
-
-
- curl_setopt($curlobj, CURLOPT_POST, 1);
- curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curlobj, CURV-HTTEAOj; =utf-8",
- "Content-length: ".strlen($data)
- ));
- curl_exec($curlobj); // 執行
- curl_setopt($curl, CURLOPT_URL, " http://www.imooc.com/space/index");
- curl_setopt($curlobj, CURLOPT_POST, 0);
- curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type:"Content-type:"
- ));
- $output=curl_redir_exec($curlobj); // 執行
- curl_close($curlobj); // 關閉cURL
- echo $output;
-
- **
- * 自訂實作頁面連結跳躍抓取
- */
- function curl_redir_exec($ch,$debug="")
- {
- static $curl_loops = 0;
- static $curl_max_loops = 20;f
- = $curl_max_loops)
- {
- $curl_loops = 0;
- return FALSE;
- }
- curl_setopt($ch, CURLOPT_HEADER, true);到重定向到的新URL
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $data = curl_exec($ch);
- // 分割返回的內容
- $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) {
- $mat = array();
- preg_match('/Location:(.ches = array();
- preg_match('/Location:(.ches = )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($,, nECT ));
- 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;
- }
- }
- ?>
複製程式碼
|