当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP封装CURL扩展类实例

PHP封装CURL扩展类实例

2018年05月10日  | 移动技术网IT编程  | 我要评论

诸城家教,三亚地图,我爱免费网

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网