PHP封装CURL扩展类实例
作者:PHP开发学习门户 发布时间:2023-07-17 15:19:50
标签:PHP,CURL,扩展
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- 这些包可以独立使用,也可以与其他包一起使用以满足复杂的业务需求。Integration Services 可以提取和转换来自多种源(如 XM
- 我们经常会遇到多重查询问题,而长长的SQL语句往往让人丈二和尚摸不着头脑。特别是客户端部分填入查询条件时,如用普通方法将更是难上加难。以下巧
- 代码如下:--相信大家肯定经常会把数据导入到数据库中,但是可能会有些记录行的所有列的数据是null,这为null的数据是我们不需要 --现在
- 序 号前 缀使用的变量/范围或数据类型1a or arrArray2b or blnBoolean3bytByte4
- 首先请把手放胸前成沉思状:我上了生活,还是被生活上了自己?没想出答案把,恩,可以读下文了。从语义角度讲,同一事物的不同表述可以反映人的主观视
- 网站可用性是任何网站的基本要素,而可用的导航更是网站所必需的要素之一。导航决定了用户如何与网站进行交互。如果没有了可用的导航,那么网站内容就
- Hihi, 大家好~ 最近有不少人都提及了网页上该如何选择字体的问题。问题虽然小,但是却是前端开发中的基本,因为目前的网页,还是以文字信息
- ASP(Active Server Page)是Microsoft公司推出的基于PWS(Personal We
- 以下是引用片段:Function c2u(myText) Dim i c2u = "" For i = 1 to Len
- <input name="a" type="checkbox"
- 一、修改操作系统核心参数在Root用户下执行以下步骤:1)修改用户的SHELL的限制,修改/etc/security/limits.conf
- 今天群上面同志们在讨论css3内发光效果,自己也就研究一下,写了个效果出来,涉及css3投影,渐变,蒙版,伪类等知识点,现在写下设计思路,仅
- 一个站点成功与否,不是在于所用的程序语言是PHP还是ASP,也不在于用BLOG建站或是CMS建站,内容和专业很重要。博客很好起步,CMS体系
- 1.弹启一个全屏窗口 <html> <body onload="win
- © 版权符号显示不清楚,就是那个圈C,在某些网站上就是显示太小,看不清楚。一开始还以为是字体大小的原因,调大以后,还是一样
- 李开复曾经在年前表示,谷歌公司会在今年春节期间,对Google谷歌的搜索结果页进行“从未有过的尝试”修改,以凸显农历春节的喜庆气氛。现在,输
- bookheader.asp Recommended Books for <%=session(&quo
- 在官网下载源码包:https://www.php.net/downloads.php步骤:1、解压命令:tar -xjvf php.tar.
- 你有没有觉得你的CSS样式表文件过于臃肿?其实如果你注意并培养一些比较好的CSS书写习惯,我想你的CSS样式表过于”肥胖”的问题会得到很好的
- 来自 Nine Javascript Gotchas , 以下是JavaScript容易犯错的九个陷阱。虽然不是什么很高深的技术问题,但注意