CURL PHP類比瀏覽器get和post

來源:互聯網
上載者:User

標籤:資料包   onload   ref   exec   echo   think   soc   .net   提交資料   

類比瀏覽器get和post資料需要經常用到的類,

在這裡收藏了幾個不錯的方法

方法一
<?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)";function vlogin($url, $data) { // 類比登入擷取Cookie函數    $curl = curl_init (); // 啟動一個CURL會話    if (IS_PROXY) {        //以下代碼設定Proxy 伺服器        //Proxy 伺服器地址        curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS [‘proxy‘] );    }    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 ); // 自動化佈建Referer    curl_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 ‘Errno‘ . curl_error ( $curl );    }    curl_close ( $curl ); // 關閉CURL會話    return $tmpInfo; // 返回資料}function vget($url) { // 類比擷取內容函數    $curl = curl_init (); // 啟動一個CURL會話    if (IS_PROXY) {        //以下代碼設定Proxy 伺服器        //Proxy 伺服器地址        curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS [‘proxy‘] );    }    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 ); // 自動化佈建Referer    curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // 發送一個常規的Post請求    curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS [‘cookie_file‘] ); // 讀取上面所儲存的Cookie資訊    curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設定逾時限制防止死迴圈    curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 顯示返回的Header地區內容    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 擷取的資訊以檔案流的形式返回    $tmpInfo = curl_exec ( $curl ); // 執行操作    if (curl_errno ( $curl )) {        echo ‘Errno‘ . curl_error ( $curl );    }    curl_close ( $curl ); // 關閉CURL會話    return $tmpInfo; // 返回資料}function vpost($url, $data) { // 類比提交資料函數    $curl = curl_init (); // 啟動一個CURL會話    if (IS_PROXY) {        //以下代碼設定Proxy 伺服器        //Proxy 伺服器地址        curl_setopt ( $curl, CURLOPT_PROXY, $GLOBALS [‘proxy‘] );    }    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 ); // 自動化佈建Referer    curl_setopt ( $curl, CURLOPT_POST, 1 ); // 發送一個常規的Post請求    curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post提交的資料包    curl_setopt ( $curl, CURLOPT_COOKIEFILE, $GLOBALS [‘cookie_file‘] ); // 讀取上面所儲存的Cookie資訊    curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // 設定逾時限制防止死迴圈    curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // 顯示返回的Header地區內容    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // 擷取的資訊以檔案流的形式返回    $tmpInfo = curl_exec ( $curl ); // 執行操作    if (curl_errno ( $curl )) {        echo ‘Errno‘ . curl_error ( $curl );    }    curl_close ( $curl ); // 關鍵CURL會話    return $tmpInfo; // 返回資料}function delcookie($cookie_file) { // 刪除Cookie函數    unlink ( $cookie_file ); // 執行刪除}?>

 

方法二
<?php/***File:curl.class.php*CURL封裝類,本類大部分操作均支援鏈式操作**example:**$curl=newCurl($url);*$curl->exec();*//發送post資料*$curl->post(array(‘username‘=>‘使用者名稱‘))->exec();*/classCurl{ private$ch; private$flag_if_have_run=false; private$has_cloase=true; publicfunction__construct($url=‘‘,$forgeIP=false){ $this->init($url,$forgeIP); } /** *初始化CURL。如果CURL未被關閉,則先關閉 * *@paramtype$url *@return\Common\Library\Curl */ publicfunctioninit($url=‘‘,$forgeIP=false){ if(!$this->has_cloase){//如果上一次串連尚未結束,則先關閉 $this->close(); } if($forgeIP){//偽造IP,將IP偽造為訪問者的IP if(Validate::isIPAddress($forgeIP)){ $ip=$forgeIP; }else{ $ip=$_SERVER[‘SERVER_ADDR‘]; } $this->set_ip($ip); } $this->ch=curl_init($url); curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1); $this->has_cloase=false; return$this; } publicfunctionsetUrl($url){ curl_setopt($this->ch,CURLOPT_URL,$url); return$this; } publicfunctionclose(){ if(!$this->has_close){ curl_close($this->ch); $this->has_cloase=true; } } publicfunction__destruct(){ $this->close(); } /** *設定頁面逾時時間,支援鏈式操作 * *@paramtype$timeout *@return\Common\Library\Curl */ publicfunctionset_time_out($timeout){ curl_setopt($this->ch,CURLOPT_TIMEOUT,intval($timeout)); return$this; } /** *偽造來源路徑 * *@paramtype$referer *@return\Common\Library\Curl */ publicfunctionset_referer($referer){ if(!empty($referer)){ curl_setopt($this->ch,CURLOPT_REFERER,$referer); } return$this; } /** *設定cookie *本方法僅發送cookie資訊到遠端,不儲存遠端返回的cookie資訊 * *@paramtype$cookie_file cookie檔案的儲存路徑 *@return\Common\Library\Curl */ publicfunctionload_cookie($cookie_file){ $this->_checkCookie($cookie_file); curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file); return$this; } /** *設定cookie *發送cookie到遠端,並儲存遠端返回的cookie * *@paramtype$cookie_file *@return\Common\Library\Curl */ publicfunctioncookie($cookie_file){ $this->_checkCookie($cookie_file); curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file); curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file); return$this; } /** *設定cookie *本方法將不發送cookie資訊,僅接收返回的cookie並儲存 * *@paramtype$cookie_file *@return\Common\Library\Curl */ publicfunctionsave_cookie($cookie_file=""){ //設定快取檔案,例如a.txt if(empty($cookie_file)){ $cookie_file=tempnam(‘./‘,‘cookie‘); } $this->_checkCookie($cookie_file); curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file); return$this; } privatefunction_checkCookie($cookie_file){ if(!\Think\Storage::has($cookie_file)){ \Think\Storage::put($cookie_file,‘‘); } } /** *執行curl請求 * *@returntype */ publicfunctionexec(){ $str=curl_exec($this->ch); $this->flag_if_have_run=true; return$str; } /** *設定發送POST請求。沒有調用過該方法預設使用GET方法提交 * *@paramtype$postData post的資料,支援數組和“xxx=1&x=2”兩種格式 *@return\Common\Library\Curl */ publicfunctionpost($postData){ curl_setopt($this->ch,CURLOPT_POST,1);//echo($postQuery);die; curl_setopt($this->ch,CURLOPT_POSTFIELDS,$postData); return$this; } /** *擷取curl的資訊 * *@returntype *@throwsException */ publicfunctionget_info(){ if($this->flag_if_have_run==true){ returncurl_getinfo($this->ch); }else{ thrownewException("<h1>需先運行(執行exec),再擷取資訊</h1>"); } } /** *設定代理 * *@paramtype$proxy *@return\Common\Library\Curl */ publicfunctionset_proxy($proxy){ //設定代理,例如‘68.119.83.81:27977‘ curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5); curl_setopt($this->ch,CURLOPT_PROXY,$proxy); return$this; } /** *佈建要求的IP * *@paramtype$ip *@returntype */ publicfunctionset_ip($ip=‘‘){ if(!empty($ip)){ curl_setopt($this->ch,CURLOPT_HTTPHEADER,array("X-FORWARDED-FOR:$ip","CLIENT-IP:$ip")); } return$ip; }}

 

CURL PHP類比瀏覽器get和post

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.