PHP封裝CURL擴(kuò)展類實(shí)例
本文實(shí)例講述了PHP封裝CURL擴(kuò)展類。分享給大家供大家參考。具體如下:
<?php /** * @description: 封裝CURL擴(kuò)展 * @date: 2014-07-28 16:04 */ /** * @編碼規(guī)范 * @class 類名首字母大寫,類名為多個(gè)單詞, 每個(gè)大字首字母大寫 eg: class Curl , class CurlPage * @variable 變量名小寫, 變量名為多個(gè)單詞, 每個(gè)單詞小寫,使用下劃線_分割 eg: $curl_result * @function 函數(shù)名與類名規(guī)則相同 eg: function SendRequest * @params 函數(shù)形參規(guī)則與變量名相同 * @class-variable 成員變量,以下劃線結(jié)尾,多個(gè)單詞使用下劃線分隔. eg: private $host_name_ */ /** * @要求 * */ class Curl{ /** * @請(qǐng)求的host */ private $host_; /** * @curl 句柄 */ private $ch_; /** * @超時(shí)限制時(shí)間 */ const time_=5; /** * @請(qǐng)求的設(shè)置 */ private $options_; /** * @保存請(qǐng)求頭信息 */ private $request_header_; /** * @保存響應(yīng)頭信息 */ private $response_header_; /** * @body_ 用于保存curl請(qǐng)求返回的結(jié)果 */ private $body_; /** * @讀取cookie */ private $cookie_file_; /** * @寫入cookie */ private $cookie_jar_; /** * @todo proxy * @構(gòu)造函數(shù),初始化CURL回話 */ public function Start($url){ $this->ch_ = curl_init($url); curl_setopt($this->ch_, CURLOPT_HEADER, 1); curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 ); } /** * @返回響應(yīng)頭 */ public function ResponseHeader($url){ if (!function_exists('http_parse_headers')) { function http_parse_headers ($raw_headers){ $headers = array(); foreach (explode("\n", $raw_headers) as $i => $h) { $h = explode(':', $h, 2); if (isset($h[1])) { if(!isset($headers[$h[0]])) { $headers[$h[0]] = trim($h[1]); } else if(is_array($headers[$h[0]])) { $tmp = array_merge($headers[$h[0]],array(trim($h[1]))); $headers[$h[0]] = $tmp; } else { $tmp = array_merge(array($headers[$h[0]]),array(trim($h[1]))); $headers[$h[0]] = $tmp; } } } return $headers; } } $this->Start($url); curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_); $this->body_=$this->Execx(); $header_size = curl_getinfo($this->ch_, CURLINFO_HEADER_SIZE); $this->response_header_ = substr($this->body_, $start = 0, $offset = $header_size); $this->response_header_ = http_parse_headers($this->response_header_); print_r($this->response_header_); return $this->Close($this->body_); } /** * @讀取cookie */ public function LoadCookie($url,$cookie_file){ $this->Start($url); curl_setopt($this->ch_, CURLOPT_COOKIE, 1); curl_setopt($this->ch_, CURLOPT_COOKIEFILE , $cookie_file); $this->body_=$this->Execx(); return $this->Close($this->body_); } /** * @寫入cookie */ public function SaveCookie($url){ $this->Start($url); curl_setopt($this->ch_, CURLOPT_COOKIE, 1); curl_setopt($this->ch_, CURLOPT_COOKIEFILE ,'cookie.txt'); curl_setopt($this->ch_, CURLOPT_COOKIEJAR , 'cookie.txt'); $this->body_=$this->Execx(); return $this->Close($this->body_); } /** * @設(shè)置HEADER */ public function SetHeader($headers = null){ if (is_array($headers) && count($headers) > 0) { curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers); } } /** * @GET請(qǐng)求 */ public function Get($url, array $params = array()) { if ($params) { if (strpos($url, '?')) { $url .= "&".http_build_query($params); } else { $url .= "?".http_build_query($params); } } $this->Start($url); curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_); if (strpos($url, 'https') === 0) { curl_setopt($this->ch_, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0); } $this->body_=$this->Execx(); return $this->Close($this->body_); } /** * @POST請(qǐng)求 */ public function Post($url, array $params = array()) { $this->Start($url); curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($this->ch_, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded")); curl_setopt($this->ch_, CURLOPT_POST, true); curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_); if ($params) { curl_setopt($this->ch_, CURLOPT_POSTFIELDS, http_build_query($params)); } $this->body_=$this->Execx(); return $this->Close($this->body_); } /** * @tips: google http head 方法 */ public function Head($url, array $params = array()) { $this->Start($url); curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_); curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 0); curl_setOpt($this->ch_,CURLOPT_NOBODY, true); $this->body_=$this->Execx(); return $this->Close($this->body_); } /** * @執(zhí)行CURL會(huì)話 */ public function Execx(){ return curl_exec($this->ch_); } /** * @關(guān)閉CURL句柄 */ public function Close($body_){ if ($body_ === false) { echo "CURL Error: " . curl_error($body_); return false; } curl_close($this->ch_); return $body_; } }
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP中如何使用Redis接管文件存儲(chǔ)Session詳解
這篇文章主要給大家介紹了關(guān)于在PHP中如何使用Redis接管文件存儲(chǔ)Session的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11PHP實(shí)現(xiàn)根據(jù)時(shí)間戳獲取周幾的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)根據(jù)時(shí)間戳獲取周幾的方法,涉及PHP針對(duì)時(shí)間與日期操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-021億條數(shù)據(jù)如何分表100張到Mysql數(shù)據(jù)庫(kù)中(PHP)
這篇文章主要介紹了當(dāng)數(shù)據(jù)量猛增的時(shí)候如何把一億條數(shù)據(jù)分表100張到Mysql數(shù)據(jù)庫(kù)中,需要的朋友可以參考下2015-07-07

比較時(shí)間段一與時(shí)間段二是否有交集的php函數(shù)

PHP正則表達(dá)式處理函數(shù)(PCRE 函數(shù))實(shí)例小結(jié)

介紹幾個(gè)array庫(kù)的新函數(shù) php