<?php
/**
* 封装基于curl的HTTP类库
*
* @file class_httplib.php
* @author qaulau@hotmail.coma
* @date 2013-8-1
*/
class httplib{
private $response;
private $response_header;
private $response_data;
private $response_info;
private $response_httpcode;
private $timeout = 30;
private $cookie = '';
private $useragent = 'PHP-HttpLib-v1.0';
private $request_header = array();
private $starttime;
private $request_proxy = array();
private $request_method;
public function __construct(){
$this->starttime = self::_microtime();
//能有效提高POST大于1M数据时的请求速度
$this->set_header('Expect');
}
/**
* 设置cookie(可选)
* @param <array|string> $cookie
*/
public function set_cookie($cookie = ''){
if (is_array($cookie)){
$cookies = array();
foreach ($cookie as $k => $v){
$cookies[] = $k.'='.$v;
}
$this->cookie .= implode('; ', $cookies);
}else{
$this->cookie .= $cookie;
}
}
/**
* 设置用户浏览器信息(可选)
* @param string $useragent : 浏览器代理信息
*/
public function set_useragent($useragent = ''){
$this->useragent = $useragent;
}
/**
* 设置头部伪造IP(可选),对某些依靠头部信息判断ip的网站有效
* @param string $ip : 需要伪造的ip
*/
public function set_forgeip($ip){
$this->set_header('CLIENT-IP',$ip);
$this->set_header('X-FORWARDED-FOR',$ip);
}
/**
* 添加请求头部信息
* @param string $k
* @param string $v
*/
public function set_header($k,$v = ''){
if (!empty($k)){
$this->request_header[] = $k.':'.$v;
}
}
/**
* 设置超时时间(可选)
* @param int $sec
*/
public function set_timeout($sec){
if($sec > ini_get('max_execution_time')) @set_time_limit($sec);
$this->timeout = $sec;
}
/**
* 设置代理(可选),如对方限制了ip访问或需要隐藏真实ip
* @param string $host :代理服务器(ip或者域名)
* @param int $port :代理服务端口
* @param string $user :用户名(如果有)
* @param string $pass :用户密码(如果有)
*/
public function set_proxy($host,$port = '',$user = '',$pass = ''){
$this->reque
采集专用php基于CURL的HTTP类库php HTTP类库
最新推荐文章于 2022-04-25 00:13:45 发布