mako\Rest


Description


REST client.


Class methods


Toggle source

public __construct($url, $options = array ( ))


Constructor.


Parameters

Type Description
string URL
array (optional) cURL options
Return value

NULL

public function __construct($url, array $options = array())
{
	$this->url     = $url;
	$this->options = $options + $this->options;
}

Toggle source

public static factory($url, $options = array ( ))


Factory method making method chaining possible right off the bat.


Parameters

Type Description
string URL
array (optional) cURL options
Return value

mako\Rest

public static function factory($url, array $options = array())
{
	return new static($url, $options);
}

Toggle source

protected execute(&$info)


Executes cURL request and returns the response.


Parameters

Type Description
array $info is filled with info about the request
Return value

string

protected function execute(& $info)
{
	// Limit the number of redirects if option is not set

	if(!isset($this->options[CURLOPT_MAXREDIRS]))
	{
		$this->options[CURLOPT_MAXREDIRS] = 5;
	}

	// Execute request

	$handle = curl_init($this->url);

	curl_setopt_array($handle, $this->options);

	$response = curl_exec($handle);

	$info = $this->info = curl_getinfo($handle);

	curl_close($handle);

	return $response;
}

Toggle source

protected parseHeaders($headers)


Parses HTTP headers and returns a nice array.http://php.net/manual/en/function.http-parse-headers.php


Parameters

Type Description
string HTTP headers
Return value

array

protected function parseHeaders($headers)
{
	if(function_exists('http_parse_headers'))
	{
		return http_parse_headers($headers);
	}

	$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $headers));

	$headers = array();

	foreach($fields as $field)
	{
		if(preg_match('/([^:]+): (.+)/m', $field, $match))
		{
			$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', function($matches){ return strtoupper($matches[0]); }, strtolower(trim($match[1])));

			if(isset($headers[$match[1]]))
			{
				$headers[$match[1]] = array($headers[$match[1]], $match[2]);
			}
			else
			{
				$headers[$match[1]] = trim($match[2]);
			}
		}
	}

	return $headers;
}

Toggle source

public info($key = NULL)


Returns info about the last cURL request.


Parameters

Type Description
string (optional) Array key
Return value

mixed

public function info($key = null)
{
	if(empty($this->info))
	{
		return false;
	}

	return ($key === null) ? $this->info : $this->info[$key];
}

Toggle source

public authenticate($username, $password, $method = -17)


Sets username and password for HTTP authentication.


Parameters

Type Description
string Username
string Password
int (optional) Authenication method
Return value

NULL

public function authenticate($username, $password, $method = CURLAUTH_ANY)
{
	$this->options[CURLOPT_HTTPAUTH] = $method;
	$this->options[CURLOPT_USERPWD]  = $username . ':' . $password;

	return $this;
}

Toggle source

public get(&$info = NULL)


Performs a GET request and returns the response.


Parameters

Type Description
array (optional) If $info is provided, then it is filled with info about the request
return sting
Return value

NULL

public function get(& $info = null)
{
	return $this->execute($info);
}

Toggle source

public head(&$info = NULL)


Performs a HEAD request and returns an array containing the response headers.


Parameters

Type Description
array (optional) If $info is provided, then it is filled with info about the request
return array
Return value

NULL

public function head(& $info = null)
{
	$this->options[CURLOPT_HEADER] = true;
	$this->options[CURLOPT_NOBODY] = true;

	return $this->parseHeaders($this->execute($info));
}

Toggle source

public post($data = array ( ), $multipart = false, &$info = NULL)


Performs a POST request and returns the response.


Parameters

Type Description
array (optional) Post data
boolean (optional) True to send data as multipart/form-data and false to send as application/x-www-form-urlencoded
array (optional) If $info is provided, then it is filled with info about the request
Return value

string

public function post(array $data = array(), $multipart = false, & $info = null)
{
	$this->options[CURLOPT_POST]       = true;
	$this->options[CURLOPT_POSTFIELDS] = ($multipart === true) ? $data : http_build_query($data);

	return $this->execute($info);
}

Toggle source

public put($data, &$info = NULL)


Performs a PUT request and returns the response.


Parameters

Type Description
mixed Put data
array (optional) If $info is provided, then it is filled with info about the request
Return value

string

public function put($data, & $info = null)
{
	$this->options[CURLOPT_CUSTOMREQUEST] = 'PUT';

	if(is_array($data))
	{
		$data = http_build_query($data);
	}

	!isset($this->options[CURLOPT_HTTPHEADER]) && $this->options[CURLOPT_HTTPHEADER] = array();

	$this->options[CURLOPT_HTTPHEADER] = array_merge($this->options[CURLOPT_HTTPHEADER], array('Content-Length: ' . strlen($data)));

	$this->options[CURLOPT_POSTFIELDS] = $data;

	return $this->execute($info);
}

Toggle source

public delete(&$info = NULL)


Performs a DELETE request and returns the response.


Parameters

Type Description
array (optional) If $info is provided, then it is filled with info about the request
Return value

string

public function delete(& $info = null)
{
	$this->options[CURLOPT_CUSTOMREQUEST] = 'DELETE';

	return $this->execute($info);
}