国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

PHP封裝CURL擴展類實例

2024-04-25 20:40:11
字體:
來源:轉載
供稿:網友

本文實例講述了PHP封裝CURL擴展類。分享給大家供大家參考。具體如下:

<?php

/**

* @description: 封裝CURL擴展

* @date: 2014-07-28 16:04

*/

/**

* @編碼規范

* @class 類名首字母大寫,類名為多個單詞, 每個大字首字母大寫 eg: class Curl , class CurlPage

* @variable 變量名小寫, 變量名為多個單詞, 每個單詞小寫,使用下劃線_分割 eg: $curl_result

* @function 函數名與類名規則相同 eg: function SendRequest

* @params 函數形參規則與變量名相同

* @class-variable 成員變量,以下劃線結尾,多個單詞使用下劃線分隔. eg: private $host_name_

*/

/**

* @要求

*

*/

class Curl{

/**

* @請求的host

*/

private $host_;

/**

* @curl 句柄

*/

private $ch_;

/**

* @超時限制時間

*/

const time_=5;

/**

* @請求的設置

*/

private $options_;

/**

* @保存請求頭信息

*/

private $request_header_;

/**

* @保存響應頭信息

*/

private $response_header_;

/**

* @body_ 用于保存curl請求返回的結果

*/

private $body_;

/**

* @讀取cookie

*/

private $cookie_file_;

/**

* @寫入cookie

*/

private $cookie_jar_;

/**

* @todo proxy

* @構造函數,初始化CURL回話

*/

public function Start($url){

$this->ch_ = curl_init($url);

curl_setopt($this->ch_, CURLOPT_HEADER, 1);

curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );

}

/**

* @返回響應頭

*/

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_);

}

/**

* @設置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_);

}

/**


* @執行CURL會話

*/

public function Execx(){

return curl_exec($this->ch_);

}

/**

* @關閉CURL句柄

*/

public function Close($body_){

if ($body_ === false) {

echo "CURL Error: " . curl_error($body_);

return false;

}

curl_close($this->ch_);

return $body_;

}

}

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昆山市| 抚顺县| 神农架林区| 芮城县| 莎车县| 宜君县| 札达县| 阿坝县| 松溪县| 长乐市| 波密县| 抚宁县| 洪湖市| 彭阳县| 盐山县| 鲁山县| 库车县| 仪征市| 资阳市| 富源县| 昌江| 长阳| 天峻县| 白河县| 贵州省| 吉林省| 秦皇岛市| 苍南县| 曲麻莱县| 个旧市| 金坛市| 尼勒克县| 乐至县| 通榆县| 德江县| 志丹县| 格尔木市| 闽侯县| 泽普县| 金溪县| 沈阳市|