//curl类
类卷曲
{
函数 Curl(){
返回真;
}
函数执行($method, $url, $fields='', $userAgent='', $httpHeaders='', $username='', $password=''){
$ch = Curl::create();
if(false === $ch){
返回错误;
}
if(is_string($url) && strlen($url)){
$ret = curl_setopt($ch, CURLOPT_URL, $url);
}其他{
返回错误;
}
// 是否显示头部信息
curl_setopt($ch, CURLOPT_HEADER, false);
//
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($用户名 != ''){
curl_setopt($ch, CURLOPT_USERPWD, $用户名 . ':' . $密码);
}
$method = strtolower($method);
if('post' == $method){
curl_setopt($ch, CURLOPT_POST, true);
if(is_array($fields)){
$sets = array();
foreach ($fields AS $key => $val){
$sets[] = $key 。 '=' 。 urlencode($val);
}
$fields = implode('&',$sets);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}else if('put' == $method){
curl_setopt($ch, CURLOPT_PUT, true);
}
//curl_setopt($ch, CURLOPT_PROGRESS, true);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
//curl_setopt($ch, CURLOPT_MUTE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数
if(strlen($userAgent)){ curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
if(is_array($httpHeaders)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
}
$ret =curl_exec($ch);
if(curl_errno($ch)){
curl_close($ch);
返回数组(curl_error($ch),curl_errno($ch));
}其他{
curl_close($ch);
if(!is_string($ret) || !strlen($ret)){
返回错误;
}
返回 $ret;
}
}
函数 post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
$ret = Curl::execute('POST', $url, $fields, $userAgent, $httpHeaders, $用户名, $password);
if(false === $ret){
返回错误;
}
if(is_array($ret)){
返回错误;
}
返回 $ret;
}
函数 get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = ''){
$ret = Curl::execute('GET', $url, '', $userAgent, $httpHeaders, $用户名, $密码);
if(false === $ret){
返回错误;
}
if(is_array($ret)){
返回错误;
}
返回 $ret;
}
函数创建(){
$ch = null;
if(!function_exists('curl_init')){
返回错误;
}
$ch =curl_init();
if(!is_resource($ch)){
返回错误;
}
返回 $ch;
}
}
?>
方便
获取帮助:
$curl = new Curl();
$curl->get('http://www.111cn.net/');
POST 处理
$curl = new Curl();
$curl->get('http://www.111cn.net/', 'p=1&time=0′);
|