REST client


Description


The REST client makes it easy to interact with RESTful web services.


Methods


__construct(string $url [, array $options = array()])


You create a Rest object by using the constructor. Use the optional options parameter to set CURL options.


$rest = new Rest('http://example.org/api');

$rest = new Rest('http://example.org/api', array(CURLOPT_CONNECTTIMEOUT => 1));

factory(string $url [, array $options = array()])


The factory method returns a Rest object. This allows you to chain methods.


$data = Rest::factory('http://example.org/api')->get();

authenticate(string $username, string $password [, int $method = CURLAUTH_ANY])


The authenticate method sets the username and password for HTTP authentication.


$data = Rest::factory('http://example.org/api')->authenticate('foo', 'bar')->get();

get([array &$info = null])


The get method performs a GET request and returns the response.


$data = Rest::factory('http://example.org/api')->get();

head([array &$info = null])


The head method performs a HEAD request and returns the response headers in a nicely formatted array.


$headers = Rest::factory('http://example.org/api')->head();

post([array $data = array() [, boolean $multipart = false [, array &$info = null]]])


The post method performs a POST request and returns the response.


$movie = array
(
	'title'    => 'Thor',
	'category' => 'Action',
);

$response = Rest::factory('http://example.org/api')->post($movie);

put(mixed $data [, array &$info = null]])


The put method performs a PUT request and returns the response.


$movie = array
(
	'title'    => 'Thor',
	'category' => 'Action',
);

$response = Rest::factory('http://example.org/api')->put(json_encode($movie));

delete([array &$info = null])


The delete method performs a DELETE request and returns the response.


$data = Rest::factory('http://example.org/api')->delete();

info([string $key = null])


The info method returns info about the last cURL request.


$rest = new Rest('http://example.org/api');

$response = $rest->get();

if($rest->info('http_code') != 200)
{
	// something went wrong
}
else
{
	// everything is ok
}

You can also set the $info parameter in the other methods to save a few lines of code.


$response = Rest::factory('http://example.org')->get($info);

if($info['http_code'] != 200)
{
	// something went wrong
}
else
{
	// everything is ok
}