The REST client makes it easy to interact with RESTful web services.
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));
The factory method returns a Rest object. This allows you to chain methods.
$data = Rest::factory('http://example.org/api')->get();
The authenticate method sets the username and password for HTTP authentication.
$data = Rest::factory('http://example.org/api')->authenticate('foo', 'bar')->get();
The get method performs a GET request and returns the response.
$data = Rest::factory('http://example.org/api')->get();
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();
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);
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));
The delete method performs a DELETE request and returns the response.
$data = Rest::factory('http://example.org/api')->delete();
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
}