php - 封装 - curl类

class CURL 
{
	private $ch;
	private $flag_if_have_run;
	
	public function __construct($url)
	{
		$this->ch = curl_init($url);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER , 1 );		
	}
	
	public function close()
	{
		curl_close($this->ch);
	}
	
	public function __destruct()
	{
		$this->close();
	}
	
	public function set_time_out($timeout)
	{
		curl_setopt($this->ch, CURLOPT_TIMEOUT, intval($timeout));
		return $this;
	}
	
	public function set_referer($referer)
	{
		if (!empty($referer))
			curl_setopt($this->ch, CURLOPT_REFERER , $referer);
		return $this;
	}
	
	public function load_cookie($cookie_file)
	{
		curl_setopt($this->ch, CURLOPT_COOKIEFILE , $cookie_file);
		return $this;
	}
	
	public function save_cookie($cookie_file="")
	{
		//设置缓存文件,例如a.txt
		if(empty($cookie_file))
			$cookie_file = tempnam('./', 'cookie');
		curl_setopt($this->ch, CURLOPT_COOKIEJAR , $cookie_file);
		return $this;
	}
	
	public function exec ()
	{
		$str = curl_exec($this->ch);
		$this->flag_if_have_run = true;
		return $str;
	}
	
	public function post ($post)
	{
		curl_setopt($this->ch, CURLOPT_POST , 1);
		curl_setopt($this->ch, CURLOPT_POSTFIELDS , $post );
		return $this;
	}
	
	public function get_info()
	{
		if($this->flag_if_have_run == true )
			return curl_getinfo($this->ch);
		else 
			throw new Exception("<h1>需先运行( 执行exec ),再获取信息</h1>");
	}
	
	public function set_proxy($proxy)
	{
		//设置代理 ,例如'68.119.83.81:27977'
		curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
		curl_setopt($this->ch, CURLOPT_PROXY,$proxy);
		return $this;
	}
	
	public function set_ip($ip)
	{
		if(!empty($ip))
			curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("X-FORWARDED-FOR:$ip", "CLIENT-IP:$ip"));
		return $ip;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值