13.2. Zend_Service_Rest

13.2.1. Introduction

Zend_Service_Rest is a basic REST client for performing queries against any REST based web service.

Example 13.1. A basic REST Request

In the following code, the setUri() method sets the base URI for the REST web service. Then, the restGet() method is called to perform a GET request on a given path with an optional query string.

<?php
require_once 'Zend/Service/Rest.php;

try {
    $rest = new Zend_Service_Rest();

    $rest->setURI('http://example.org');

    // Returns a Zend_HttpClient_Response Object
    $response = $rest->restGet('/services/rest', 'foo=bar&baz=bat');

    if ($response->isSuccessful()) {
        echo $response->getBody();
    } else {
        echo '<p>An error occurred</p>';
    }
} catch (Zend_Exception $e) {
        echo '<p>An error occurred (' .$e->getMessage(). ')<p>';
}
?>       
[Note] Note
You can pass a properly formatted query string (without the leading question mark "?") to restGet(), or you can pass an associative array of name-value pairs. Regardless of which method you choose, the names and values must be URL-encoded (e.g., with urlencode()).

Using restPost() and restPut() is the same as using restGet(), but the data are sent via HTTP POST or PUT, respectively, instead of GET.

The restDelete() method operates on a path, but does not have a second parameter into which data may be passed.