php curl cookie 存取樣本(標準採集程式)及偽裝

來源:互聯網
上載者:User

php curl cookie 存取樣本

好多人發來訊息詢問curl存取cookie檔案的問題,杜工並不覺得這是個痛點,因為只看手冊就可以很容易把握。下面給個例子,看完後就全都明了了:

<?php$cookie_jar_index = dir(__FILE__)."/".'cookie.txt'; $url = "http://www.71j.cn/perl/login.pl";$params = "username=dudu&password=****";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_index);//curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple; colour=red");//上面代碼是直接傳遞cookie資訊,而非檔案curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $params); //curl_setopt($ch, CURLOPT_NOBODY, 1);//這個不能開啟,否則無法產生cookie檔案ob_start();curl_exec($ch);curl_close($ch);ob_clean(); $url = "http://www.71j.cn/perl/myfavorites.pl";$ch2 = curl_init();curl_setopt($ch2, CURLOPT_URL, $url);curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar_index);ob_start();curl_exec($ch2);curl_close($ch2);$rs = ob_get_contents(); //$rs就是返回的內容ob_clean(); print_r($rs); ?>

CURL IP偽裝,更改IP頭資訊

<?php$ip = rand(1,255).".".rand(1,255).".".rand(1,255).".".rand(1,255)."";$ip='8.8.8.8';$ch = curl_init();curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);curl_setopt($ch,CURLOPT_HTTPHEADER,array("Client_Ip: ".$ip."", "Real_ip: ".$ip."", "X_FORWARD_FOR:".$ip."", "X-forwarded-for: ".$ip."", "PROXY_USER: ".$ip.""));curl_setopt($ch, CURLOPT_URL,"http://192.168.1.99/11.php");echo curl_exec($ch);curl_close($ch);?>


進階篇:

CURLOPT_PROXY這個參數是否真的使用代理去請求資料。那他跟偽裝IP的區別有哪些。下面我們做個實驗。

curl_proxy_test.php

<?phpdefine ('IS_PROXY', true); //是否啟用代理/* cookie檔案 */$cookie_file = dirname ( __FILE__ ) . "/cookie_" . md5 ( basename ( __FILE__ ) ) . ".txt"; // 設定Cookie檔案儲存路徑及檔案名稱/*類比瀏覽器*/$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";$proxy = "61.135.169.125";function vlogin($url, $data) { // 類比登入擷取Cookie函數$curl = curl_init (); // 啟動一個CURL會話if (IS_PROXY) {//以下代碼設定Proxy 伺服器//Proxy 伺服器地址curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS ['proxy'] );}// IP偽裝$ip = rand(1,255).".".rand(1,255).".".rand(1,255).".".rand(1,255)."";  curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //構造IP  curl_setopt($curl, CURLOPT_REFERER, "http://www.nowamagic.net/ ");   //構造來路  curl_setopt($curl, CURLOPT_HEADER, 1);curl_setopt ( $curl, CURLOPT_URL, $url ); // 要訪問的地址curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // 對認證認證來源的檢查curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // 從認證中檢查SSL密碼編譯演算法是否存在curl_setopt ( $curl, CURLOPT_USERAGENT, $GLOBALS ['user_agent'] ); // 類比使用者使用的瀏覽器@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // 使用自動跳轉curl_setopt ( $curl, CURLOPT_AUTOREFERER, 1 ); // 自動化佈建Referercurl_setopt ( $curl, CURLOPT_POST, 1 ); // 發送一個常規的Post請求curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的資料包curl_setopt ( $curl, CURLOPT_COOKIEJAR, $GLOBALS ['cookie_file'] ); // 存放Cookie資訊的檔案名稱curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file'] ); // 讀取上面所儲存的Cookie資訊curl_setopt ( $curl, CURLOPT_TIMEOUT, 30 ); // 設定逾時限制防止死迴圈curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 顯示返回的Header地區內容curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 擷取的資訊以檔案流的形式返回$tmpInfo = curl_exec ( $curl ); // 執行操作if (curl_errno ( $curl )) {echo 'Error ' . curl_error ( $curl );}curl_close ( $curl ); // 關閉CURL會話return $tmpInfo; // 返回資料}$params = "username=dudu&password=****";$result = vlogin("http://demo2.test.com/curl_proxy_test2.php",$params);echo $result;?>

curl_proxy_test2.php

<?phperror_reporting("E_ALL ^ (E_NOTICE | E_WARNING)");function getClientIp() {  if (!empty($_SERVER["HTTP_CLIENT_IP"]))  $ip = $_SERVER["HTTP_CLIENT_IP"];  else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))  $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];  else if (!empty($_SERVER["REMOTE_ADDR"]))  $ip = $_SERVER["REMOTE_ADDR"];  else  $ip = "err";return $ip;  }echo "<br />IP: " . getClientIp() . "<br />";  print_r($_SERVER);  ?>

執行http://demo2.test.com/curl_proxy_test.php

輸出內容為:Error couldn't connect to host

表明你必須要有台Proxy 伺服器才能完成操作。說到偽裝跟代理的區別時,偽裝會被識破的,只有使用匿名代理才能達到真正代理的效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.