<?php

function_exists('curl_init') or die('Модуль cURL недоступен');


class curl{
var $ua = 'DCMS/import'; // USER AGENT
var $timeout = 10; // таймаут запроса
var $location = false; // обработка переадресаций
var $referer = '';

protected $curl;
protected $url;
protected $post = array();
protected $cookie = array();


function __construct($url)
{
$this->url=$url;
$this->curl = curl_init($this->url);
}

function set_post($name,$value=null)
{
$this->post[]=$name.'='.urlencode($value);
}

function set_cookie($name,$value=null)
{
$this->cookie[]=$name.'='.urlencode($value);
}

public function save_content($file)
{
$f=@fopen($file, 'w');
$this->options();
curl_setopt ($this->curl, CURLOPT_HEADER, false);
curl_setopt ($this->curl, CURLOPT_NOBODY, false);
curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt ($this->curl, CURLOPT_FILE, $f);
$result=curl_exec($this->curl);
fclose($f);
return $result;
}

// получение содержимого
public function get_content()
{
$this->options();
curl_setopt ($this->curl, CURLOPT_HEADER, false);
curl_setopt ($this->curl, CURLOPT_NOBODY, false);
curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, true);
return curl_exec($this->curl);
}

// получение заголовков
public function get_headers()
{
$this->options();
curl_setopt ($this->curl, CURLOPT_HEADER, true);
curl_setopt ($this->curl, CURLOPT_NOBODY, true);
curl_setopt ($this->curl, CURLOPT_RETURNTRANSFER, true);
return curl_exec($this->curl);
}


protected function options()
{

if ($this->post)
{
curl_setopt ($this->curl, CURLOPT_POST, true);
curl_setopt ($this->curl, CURLOPT_POSTFIELDS, implode('&',$this->post));
}

if ($this->cookie)
{
curl_setopt ($this->curl, CURLOPT_COOKIE, implode(';',$this->cookie));
}


curl_setopt ($this->curl, CURLOPT_USERAGENT, $this->ua);
curl_setopt ($this->curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt ($this->curl, CURLOPT_FOLLOWLOCATION, $this->location);
curl_setopt ($this->curl, CURLOPT_REFERER, $this->referer);
}
}
?>