php_curl类
(2006-07-08) 回首页
<?php
/**
* curl访问
*
* @author xiongc <superspice AT yeah.net>
* @date 2006-07-08
*
*/
class httpCrossDomain
{
/**
* 初始化的curl连接
*
* @var object
*/
var $ch;
/**
* curl_exec的返回值
*
* @var string
*/
var $return;
/**
* 返回header里的http_code
*
* @var string
*/
var $headerCode;
/**
* 错误信息记录
*
* @var array
*/
var $handlerError = array();
/**
* 浏览器错误信息定义
*
* @var array
*/
var $httpCodeInfoList = array("200" => "OK",
"201" => "Created",
"202" => "Accepted",
"203" => "Non-Authoritative Information",
"204" => "No Content",
"205" => "Reset Content",
"206" => "Partial Content",
"300" => "Multiple Choices",
"301" => "Moved Permanently",
"302" => "Found",
"303" => "See Other",
"304" => "Not Modified",
"305" => "Use Proxy",
"306" => "(Unused)",
"307" => "Temporary Redirect",
"400" => "Bad Request",
"401" => "Unauthorized",
"402" => "Payment Required",
"403" => "Forbidden",
"404" => "Not Found",
"405" => "Method Not Allowed",
"406" => "Not Acceptable",
"407" => "Proxy Authentication Required",
"408" => "Request Timeout",
"409" => "Conflict",
"410" => "Gone",
"411" => "Length Required",
"412" => "Precondition Failed",
"413" => "Request Entity Too Large",
"414" => "Request-URI Too Long",
"415" => "Unsupported Media Type",
"416" => "Requested Range Not Satisfiable",
"417" => "Expectation Failed",
"500" => "Internal Server Error",
"501" => "Not Implemented",
"502" => "Bad Gateway",
"503" => "Service Unavailable",
"504" => "Gateway Timeout",
"505" => "HTTP Version Not Supported");
/**
* 构造函数,初始化一个curl连接
*
* @param string $url 连接地址
* @param array $header 给于的http头文件信息
* @param string $method 提交方式
* @param string $queryString 提交变量
* @param int $timeOut 连接超时时间
*/
function httpCrossDomain()
{
$this->ch = curl_init();
}
/**
* 设置一些参数
*
*/
function httpsetOption($url, $header = "", $method = "get", $queryString = "", $timeOut = 30)
{
if($this->ch)
{
curl_setopt($this->ch, CURLOPT_URL, $url);
if($header)
{
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
}
if($method == "post")
{
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $queryString);
}
curl_setopt($this->ch, CURLOPT_HEADER, 0);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeOut);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
}
else
{
$this->handlerError[] = "无法建立和" . $url ."的curl连接";
return false;
}
}
/**
* 设置http访问的用户名和密码
*
*/
function httpSetAuthUserPasswd($UserPasswd)
{
if(ereg(":", $UserPasswd) && strpos($UserPasswd, ":") !== 0 && strpos($UserPasswd, ":") !== strlen($UserPasswd))
{
return false;
}
else
{
curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $UserPasswd);
return true;
}
}
/**
* 执行
*
*/
function httpExec()
{
$this->return = curl_exec($this->ch);
}
/**
* 得到返回值,不包含网页header信息的代码内容
*
*/
function httpFetchContent()
{
$this->headerCode = $this->httpGetHeaderCode();
if(!$this->ch)
{
$this->handlerError[] = "curl连接未建立,无法获取返回内容";
return false;
}
elseif(substr($this->headerCode, 0, 1) !== "2")
{
$this->handlerError[] = "http返回非2**的代码:" . $this->headerCode . "(" . $this->httpCodeInfoList[$this->headerCode] . ")";
return false;
}
else
{
return $this->return;
}
}
/**
* 得到返回header中的httpd_code,以次判断本次连接状态
*
*/
function httpGetHeaderCode()
{
if($this->ch)
{
$info = curl_getinfo($this->ch);
if($info)
{
$this->headerCode = $info['http_code'];
return $this->headerCode;
}
else
{
$this->handlerError[] = "意外错误,http_code返回为空";
return false;
}
}
else
{
$this->handlerError[] = "curl连接未建立,无法获取http_code";
return false;
}
}
/**
* 关闭连接
*
*/
function httpCloseHandler()
{
if($this->ch)
{
curl_close($this->ch);
return true;
}
else
{
$this->handlerError[] = "无法关闭curl连接";
return false;
}
}
}
?>