Wednesday, 21 February 2007
An Easier XML-RPC for PHP 5 |
| |
|
| |
Mike Naberezny, in a post over his blog, informs you about an easier XML-RPC client for PHP 5. He says, that he rewrote the Zend XML-RPC client as a part of a customer project. After fixing a fair number of bugs and writing a test suite, he made some enhancements and usability improvements. A detailed explanation on the new XML-RPC client can be found in the documentation, he informs. He briefly discusses them in the following sections:
Calling Remote Methods
Mike says the new XML-RPC client has always functioned similarly to many existing PHP implementations, providing a call() method as follows:
= new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
echo ->call('test.sayHello');
He explains that the call() instance method accepts an optional parameter with an array of parameters to marshal to the remote method. These may be native PHP types or PHP objects representing XML-RPC types. The latter is useful for the XML-RPC data types that do not map directly to PHP equivalents, such as base64 he points out.
Server Proxy Objects
He comments that the above usage works fine for many purposes but could read easier and gets tedious after many method calls. He says that one of the few advantages of serializing method calls with a protocol like XML-RPC or SOAP is that with a little extra work in the client libraries, the remote service can be exposed in a way that’s very close to a native PHP object. This is where the server proxy comes in, he says.
In the above example of test.sayHello(), the remote sayHello() method is in the XML-RPC pseudo-namespace test. We can use the new XML-RPC client’s getProxy() method to get a proxy to this remote namespace and then use it similarly to a normal PHP object, he says.
= new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc');
= ->getProxy('test'); echo ->sayHello();
Namespaces may be nested to any depth so the XML-RPC method foo.bar.baz() becomes ->bar->baz().
Faults
He explains that faults resulting from the remote method call are automatically thrown as PHP exceptions. XML-RPC fault responses are thrown as Zend_XmlRpc_FaultException and transport errors are thrown as Zend_XmlRpc_HttpException. If this is not desirable for some reason, a doRequest() method provides a way to work with request and response objects directly, he initiates.
|
| |
|
Read the Post
|
| |
|
|
| |
|
|
| |
|