Easier XML-RPC for PHP 5
A few weeks ago, I rewrote the Zend XML-RPC client. After fixing some bugs and writing a test suite, I made some enhancements and usability improvements. You can learn more about the new XML-RPC client from its documentation. Here it is in a nutshell:
Calling Remote Methods
The new XML-RPC client has always functioned similarly to many existing PHP implementations, providing a call() method:
$c = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc'); echo $c->call('test.sayHello');
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 datatypes that do not map directly to PHP equivalents, such as
Server Proxy Objects
The above usage works fine for many purposes but could read easier and gets tedious after many method calls. 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.
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.
$c = new Zend_XmlRpc_Client('http://framework.zend.com/xmlrpc'); $test = $c->getProxy('test'); echo $test->sayHello();
Namespaces may be nested to any depth so the XML-RPC method
Faults resulting from the remote method call are automatically thrown as PHP exceptions. XML-RPC fault responses are thrown as
Zend_XmlRpc_FaultExceptionand 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.
Update: This post was featured on PHPDeveloper.org.