PHP封裝CURL擴展類實例
更新時間:2015年07月28日 16:02:39 作者:PHP開發(fā)學(xué)習(xí)門戶
這篇文章主要介紹了PHP封裝CURL擴展類,實例分析了基于curl發(fā)送post、get請求及操作cookie等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了PHP封裝CURL擴展類。分享給大家供大家參考。具體如下:
<?php
/**
* @description: 封裝CURL擴展
* @date: 2014-07-28 16:04
*/
/**
* @編碼規(guī)范
* @class 類名首字母大寫,類名為多個單詞, 每個大字首字母大寫 eg: class Curl , class CurlPage
* @variable 變量名小寫, 變量名為多個單詞, 每個單詞小寫,使用下劃線_分割 eg: $curl_result
* @function 函數(shù)名與類名規(guī)則相同 eg: function SendRequest
* @params 函數(shù)形參規(guī)則與變量名相同
* @class-variable 成員變量,以下劃線結(jié)尾,多個單詞使用下劃線分隔. eg: private $host_name_
*/
/**
* @要求
*
*/
class Curl{
/**
* @請求的host
*/
private $host_;
/**
* @curl 句柄
*/
private $ch_;
/**
* @超時限制時間
*/
const time_=5;
/**
* @請求的設(shè)置
*/
private $options_;
/**
* @保存請求頭信息
*/
private $request_header_;
/**
* @保存響應(yīng)頭信息
*/
private $response_header_;
/**
* @body_ 用于保存curl請求返回的結(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請求
*/
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請求
*/
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會話
*/
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_;
}
}
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
php實現(xiàn)購物車功能(以大蘋果購物網(wǎng)為例)
本文主要介紹了php實現(xiàn)購物車功能(以大蘋果購物網(wǎng)為例)的實現(xiàn)方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
PHP獲取當(dāng)前系統(tǒng)時間的方法小結(jié)
這篇文章主要介紹了PHP獲取當(dāng)前系統(tǒng)時間的方法小結(jié),需要的朋友可以參考下2018-10-10
PHP檢測數(shù)據(jù)類型的幾種方法(總結(jié))
下面小編就為大家?guī)硪黄狿HP檢測數(shù)據(jù)類型的幾種方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
PHP實現(xiàn)設(shè)計模式中的抽象工廠模式詳解
這篇文章主要介紹了PHP實現(xiàn)設(shè)計模式中的抽象工廠模式詳解,抽象工廠模式(Abstact Factory)是一種常見的軟件設(shè)計模式,需要的朋友可以參考下2014-10-10
PHP使用內(nèi)置函數(shù)file_put_contents寫入文件及追加內(nèi)容的方法
這篇文章主要介紹了PHP使用內(nèi)置函數(shù)file_put_contents寫入文件及追加內(nèi)容的方法,結(jié)合實例形式分析了file_put_contents函數(shù)通過參數(shù)設(shè)置實現(xiàn)寫入文件及追加內(nèi)容的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下2015-12-12
Mac下關(guān)于PHP環(huán)境和擴展的安裝詳解
今天小編就為大家分享一篇Mac下關(guān)于PHP環(huán)境和擴展的安裝詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
php?ZipArchive解壓縮實現(xiàn)后臺管理升級問題詳解
php?ZipArchive可以說是php自帶的一個函數(shù)了,他可對對文件進行壓縮與解壓縮處理,但是使用此類之前我們必須在php.ini中把extension=php_zip.dll前面的分號有沒有去掉,然后再重啟Apache這樣才能使用這個類庫2022-12-12
php array_intersect()函數(shù)使用代碼
array_intersect() 返回一個數(shù)組,該數(shù)組包含了所有在 array1 中也同時出現(xiàn)在所有其它參數(shù)數(shù)組中的值。注意鍵名保留不變。2009-01-01

