1. 程式人生 > >PHP curl封裝類

PHP curl封裝類

<?php /** * @author askwei **/ class CURL { private $ch; private $url = "http://www.baidu.com"; private $flag_if_have_run; //標記exec是否已經執行 private $set_time_out = 20; //設定curl超時時間 private $cookie_file = ""; //cookie_file路徑 private $cookie_mode = 0; //cookie儲存模式 0不使用 1客戶端、2伺服器檔案
private $show_header = 0; //是否輸出返回頭資訊 private $set_useragent = ""; //模擬使用者使用的瀏覽器,預設為模擬 //建構函式 public function __construct($url = ""){ $this->ch = curl_init(); $this->url = $url ? $url : $this->url; //$this->set_useragent = $_SERVER['HTTP_USER_AGENT']; // 模擬使用者使用的瀏覽器
$this->set_useragent ="Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/7.0 Mobile/10B350 Safari/9537.53"; // $this->set_useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36";
//$this->cookie_file=dirname(__FILE__)."/cookie_".md5(basename(__FILE__)).".txt"; //初始化cookie檔案路徑 //$this->cookie_file= SAE_TMP_PATH.TmpFS; $this->cookie_file = "saekv://cookie_2014.txt"; } //關閉curl public function close(){ curl_close($this->ch); } //解構函式 public function __destruct(){ $this->close(); } //設定超時 public function set_time_out($timeout=20){ if(intval($timeout) != 0) $this->set_time_out = $timeout; return $this; } //設定來源頁面 public function set_referer($referer = ""){ if (!empty($referer)) curl_setopt($this->ch, CURLOPT_REFERER , $referer); return $this; } //設定cookie存放模式 1客戶端、2伺服器檔案 public function set_cookie_mode($mode = ""){ $this->cookie_mode = $mode; return $this; } //載入cookie public function load_cookie(){ if($this->cookie_mode == 1 ) { if(isset($_COOKIE['curl'])){ curl_setopt($this->ch,CURLOPT_COOKIE,$_COOKIE['curl']); }else{ $this->exec(); curl_setopt($this->ch,CURLOPT_COOKIE,$this->cookie_file); } } if($this->cookie_mode == 2 ) { curl_setopt($this->ch, CURLOPT_COOKIEFILE , $this->cookie_file); } if($this->cookie_mode == 3 ) { $kv = new SaeKV(); $ret = $kv->init(); $ret = $kv->get('curl_cookie'); if($ret) curl_setopt($this->ch,CURLOPT_COOKIE, $ret); } return $this; } //設定儲存cookie方式 $cookie_val 模式1為變數 模式2為檔案路徑 public function save_cookie($cookie_val = "") { //儲存在客戶端 if($this->cookie_mode == 1 && $cookie_val){ setcookie('curl',$cookie_val); } //儲存伺服器端 if($this->cookie_mode == 2){ if(!empty($cookie_val)) $this->cookie_file = $cookie_val; curl_setopt($this->ch, CURLOPT_COOKIEJAR , $this->cookie_file); } //儲存在sae if($this->cookie_mode == 3 && $cookie_val){ $kv = new SaeKV(); $ret = $kv->init(); $ret = $kv->get('curl_cookie'); if($ret){ $ret = $kv->set('curl_cookie', $cookie_val ); }else{ $ret = $kv->add('curl_cookie', $cookie_val); } } return $this; } //post引數 (array) $post public function post ($post = ""){ if($post && is_array($post)){ curl_setopt($this->ch, CURLOPT_POST , 1); curl_setopt($this->ch, CURLOPT_POSTFIELDS , $post ); } return $this; } //設定代理 ,例如'68.119.83.81:27977' public function set_proxy($proxy = ""){ if($proxy){ curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); curl_setopt($this->ch, CURLOPT_PROXY,$proxy); } return $this; } //設定偽造ip public function set_ip($ip=""){ if(!empty($ip)) curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("X-FORWARDED-FOR:$ip", "CLIENT-IP:$ip")); return $ip; } //設定是否顯示返回頭資訊 public function show_header($show=0){ $this->show_header = 0; if($show) $this->show_header = 1; return $this; } //設定請求頭資訊 public function set_useragent($str=""){ if($str) $this->set_useragent = $str; return $this; } //執行 public function exec ($url = ""){ if(!$url) $url = $this->url; curl_setopt($this->ch, CURLOPT_URL, $url); // 要訪問的地址 curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 對認證證書來源的檢查 curl_setopt($this->ch, CURLOPT_RETURNTRANSFER , 1 ); //獲取的資訊以檔案流的形式返回 curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密演算法是否存在 curl_setopt($this->ch, CURLOPT_USERAGENT, $this->set_useragent); // 模擬使用者使用的瀏覽器 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉 curl_setopt($this->ch, CURLOPT_AUTOREFERER, 1); // 自動設定Referer curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->set_time_out); //超時設定 curl_setopt($this->ch, CURLOPT_HEADER, $this->show_header); // 顯示返回的Header區域內容 curl_setopt($this->ch, CURLOPT_NOBODY, 0);//不返回response body內容 $res = curl_exec($this->ch); $this->flag_if_have_run = true; if (curl_errno($this->ch)) { //echo 'Errno'.curl_error($this->ch); return false; } if($this->show_header == 1){ //陣列形式返回頭資訊和body資訊 list($header, $body) = explode("\r\n\r\n", $res); $arr['header'] = $header; $arr['body'] = $body; if($this->cookie_mode == 1 || $this->cookie_mode == 3){ preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches); //print_r($matches); if($matches && isset($matches[1]) ){ $val = implode(';',array_unique(explode(';',implode(';',$matches[1])))); //去重處理 if($val) $this->save_cookie($val); //設定客戶端儲存cookie } } if($arr) return $arr; } return $res; } //返回 curl_getinfo資訊 public function get_info(){ if($this->flag_if_have_run == true ) return curl_getinfo($this->ch); else throw new Exception("<h1>需先執行( 執行exec ),再獲取資訊</h1>"); } } ?>