fracasula
8/14/2013 - 3:08 PM

Basic curl class

Basic curl class

<?php

	$curl = new Curl();
	$curl->initialize('http://www.example.com/');

	$curl->param1 = 'data 1';
	$curl->param2 = 'data 2';

	$curl->postRequest();

	echo $curl;
<?php

	class Curl
	{
		const HTTP_OK = 200;
		const HTTP_NOT_FOUND = 404;
		const HTTP_INTERNAL_SERVER_ERROR = 500;

		const TEXT_HTML = 'text/html';
		const TEXT_PLAIN = 'text/plain';

		protected $ch;
		protected $url;
		protected $data;
		protected $files;
		protected $result;
		protected $port = 80;
		protected $verbose = false;
		protected $headers;

		protected $tor = false;
		protected $user_agent;
		protected $request_headers;

		public function initialize($url)
		{
			$this->ch = curl_init();
			$this->url = trim($url);

			$this->data = array();
			$this->files = array();

			$this->request_headers = array
			(
				"Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
				"Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
				"Accept-Encoding" => "gzip,deflate,sdch",
				"Accept-Language" => "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4",
				"Cache-Control" => "max-age=0",
				"Connection" => "keep-alive",
				//'Content-Type' => 'multipart/form-data; charset=utf-8'
			);

			$this->user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
		}

		public function __destruct()
		{
			if ( is_resource($this->ch) )
				curl_close($this->ch);

			$this->url = null;
			unset($this->url);

			$this->data = null;
			$this->files = null;
			$this->result = null;
			$this->headers = null;
		}

		public function __set($att, $value)
		{
			$this->data[$att] = $value;
		}

		public function __get($att)
		{
			if (isset($this->data[$att]))
				return $this->data[$att];
			else
				throw new Exception(__METHOD__ . ": Attribute $att doesn't exists");
		}

		public function __toString()
		{
			return strval($this->result);
		}

		private function setPostOpts()
		{
			foreach ($this->files as $k => $file)
				$this->data['_file_' . $k] = '@' . $file;

			curl_setopt($this->ch, CURLOPT_POST, true);
			curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data);
		}

		private function setGetOpts()
		{
			curl_setopt($this->ch, CURLOPT_HTTPGET, true);
		}

		private function setOpts($waitResponse = true)
		{
			$header = array();
			foreach ($this->request_headers as $key => $value)
				$header[] = "{$key}: {$value}";

			curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
			curl_setopt($this->ch, CURLOPT_USERAGENT, $this->user_agent);
			curl_setopt($this->ch, CURLOPT_URL, $this->url);
			curl_setopt($this->ch, CURLOPT_HEADER, false);
			curl_setopt($this->ch, CURLINFO_HEADER_OUT, true);
			curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
			curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, (bool) $waitResponse);
			curl_setopt($this->ch, CURLOPT_VERBOSE, (bool) $this->verbose);
			curl_setopt($this->ch, CURLOPT_PORT, $this->port);
			curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
			curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);
			curl_setopt($this->ch, CURLOPT_ENCODING, '');
			curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
			curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);
			curl_setopt($this->ch, CURLOPT_DNS_CACHE_TIMEOUT, 120);
			curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);
			curl_setopt($this->ch, CURLOPT_COOKIEFILE, 'cookie');
		}

		public function setCredentials($user, $pass)
		{
			curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
			curl_setopt($this->ch, CURLOPT_USERPWD, "{$user}:{$pass}");
		}

		public function postRequest($waitResponse = true)
		{
			$this->setOpts($waitResponse);
			$this->setPostOpts();

			$this->result = curl_exec($this->ch);

			if (curl_errno($this->ch))
				echo curl_error($this->ch);

			return $this->result;
		}

		public function getRequest()
		{
			$this->setOpts();
			$this->setGetOpts();
			$this->result = curl_exec($this->ch);
			$this->headers = curl_getinfo($this->ch, CURLINFO_HEADER_OUT);

			return $this->result;
		}

		public function getHeaders()
		{
			return $this->headers;
		}

		public function getRealUrl()
		{
			return curl_getinfo($this->ch, CURLINFO_EFFECTIVE_URL);
		}

		public function getHttpCode()
		{
			return curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
		}

		public function getContentType()
		{
			return curl_getinfo($this->ch, CURLINFO_CONTENT_TYPE);
		}

		public function sendFile($file)
		{
			if (!file_exists($file))
				throw new Exception(__METHOD__ . ": File $file doesn't exist");
			else
				$this->files[] = trim($file);
		}

		public function setPort($port = 80)
		{
			if ($port >= 1 || $port <= 65535)
				$this->port = (int) $port;
			else
				throw new Exception('Invalid port specified');
		}

		public function setProxy($proxy, $port)
		{
			if ( !is_string($proxy) )
				throw new Exception ('Invalid proxy specified');

			curl_setopt($this->ch, CURLOPT_HTTPPROXYTUNNEL, true);
			curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
			curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
			curl_setopt($this->ch, CURLOPT_PROXYPORT, intval($port));
		}

		public function useTor()
		{
			$this->tor = true;
			$this->setProxy('127.0.0.1', 9050);
		}

		public function verbose($v)
		{
			$this->verbose = (bool) $v;
		}

		public function setUserAgent($ua)
		{
			$this->user_agent = trim( (string) $ua );
		}

		public function getUserAgent()
		{
			$this->user_agent;
		}

		public function setCustomHeaderRow($headerRow)
		{
			$key = key($headerRow);

			if ( is_string($key) )
			{
				$this->request_headers[$key] = $headerRow[$key];
				return true;
			}

			return false;
		}
	}