PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Understanding the Proxy

In the previous example, we used the getService() method to return a proxy object to the IMFS namespace. The proxy object allows you to use the Nirvanix REST service in a way that's closer to making a normal PHP method call, as opposed to constructing your own HTTP request objects.

A proxy object may provide convenience methods. These are methods that the Zend_Service_Nirvanix provides to simplify the use of the Nirvanix web services. In the previous example, the methods putContents(), getContents(), and unlink() do not have direct equivalents in the REST API. They are convenience methods provided by Zend_Service_Nirvanix that abstract more complicated operations on the REST API.

For all other method calls to the proxy object, the proxy will dynamically convert the method call to the equivalent HTTP POST request to the REST API. It does this by using the method name as the API command, and an associative array in the first argument as the POST parameters.

Let's say you want to call the REST API method RenameFile, which does not have a convenience method in Zend_Service_Nirvanix:

<?php
$auth 
= array('username' => 'your-username',
              
'password' => 'your-password',
              
'appKey'   => 'your-app-key');

$nirvanix = new Zend_Service_Nirvanix($auth);
$imfs $nirvanix->getService('IMFS');

$result $imfs->renameFile(array('filePath' => '/path/to/foo.txt',
                                  
'newFileName' => 'bar.txt'));

Above, a proxy for the IMFS namespace is created. A method, renameFile(), is then called on the proxy. This method does not exist as a convenience method in the PHP code, so it is trapped by __call() and converted into a POST request to the REST API where the associative array is used as the POST parameters.

Notice in the Nirvanix API documentation that sessionToken is required for this method but we did not give it to the proxy object. It is added automatically for your convenience.

The result of this operation will either be a Zend_Service_Nirvanix_Response object wrapping the XML returned by Nirvanix, or a Zend_Service_Nirvanix_Exception if an error occurred.

Zend Framework