成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁 ? 技術(shù)教程 ? PHP封裝CURL擴展類實例
全站頭部文字 我要出現(xiàn)在這里
PHP封裝CURL擴展類實例 771 2023-12-08   

本文實例講述了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è)計有所幫助。


主站蜘蛛池模板: 美式壁纸| 鹤壁旅游必去十大景点| 海南岛全景图| 郑丽身高一米几| 守卫者2| 月亮电影| 睡衣派对| 现代古诗冰心| 不留痕迹| 浙江卫视节目表 今晚| 申请采购的请示范文| 欧美一级黄色录像| 微信头像图片2024最新| 芝加哥急救| 肖央喜剧电影《情圣》| 电影井冈山| 2024微信头像图片| 欧美一级毛片免费看| 最爱电影完整版在线观看免费高清| 《求知报》答案| 卫星掉落| 香港艳情电影| 浙江卫视全天节目表| 寻梦环游记电影| 东莞久久精工机械有限公司| 杨幂一级毛片在线播放| 郭明翔| 女生被艹在线观看| 幸福花园在线观看| 床上视频网站| 日本电影家庭教师| 吸油记游戏破解版无限金币| 电影不见不散| 12月日历2024日历表| 欢乐钓鱼大师兑换码| 金花瓶梅花2的剧情简介 | 花守由美里| 《致青春》电影| 好男当家 电视剧| 南来北往连续剧免费观看完整版| 速度与激情 电影|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103