Using XML_RPC2 with PEAR - Creating an XML-RPC Client
(Page 2 of 4 )
We'll start with a simple client example using a freely available XML-RPC service:
<?
// Include the XML_RPC2 Client library
require('XML/RPC2/Client.php');
// freshmeat.net provides an API for getting into about open source
// projects
$client = XML_RPC2_Client::create('http://freshmeat.net/xmlrpc/');
try {
// this call lists all the open-source licenses they accept
$result = $client->fetch_available_licenses();
// The return is a simple <array />, which the client
// automatically converts into a PHP structure, no parsing
// necessary
print '<ul>';
foreach ($result as $license) {
printf('<li>%s</li>', $license);
}
print '</ul>';
}
catch (XML_RPC2_FaultException $e) {
// This exception is thrown for parsing errors, calling invalid
// methods, bad arguments and other protocol level problems
print "XML-RPC request faulted (" . $e->getFaultString() . ")";
}
catch (Exception $e) {
// Other exceptions may be thrown for networking or other issues
print "XML-RPC request failed (" . $e->getMessage() . ")";
}
?>
Assuming everything is installed properly, you should be able to run the above example and it will receive output similar to this:
Academic Free License (AFL)
Adaptive Public License (APL)
Affero General Public License
Aladdin Free Public License (AFPL)
Apple Public Source License (APSL)
Artistic License
Boost Software License
BSD License (original)
...
As you can see, the XML_RPC2 client takes care of formatting the request, transmitting the XML packet to the server and parsing the response. It makes the process very simple. Additionally, you can see how the client makes use of PHP5's advanced object-oriented capabilities to provide an incredibly simple interface. Every method available from the XML-RPC server is provided as a method call on the client object.
In our next example, we'll use geocoder.us, which provides a free XML-RPC service for geocoding addresses and intersections. You pass in an address string and they will look up that address and return the canonical address along with the latitude and longitude of that location.
<?php
require('XML/RPC2/Client.php');
$client = XML_RPC2_Client::create('http://geocoder.us/service/xmlrpc');
try {
// the geocode methods takes an address and attempts to validate
// and locate that address
$result = $client->geocode('1600 Pennsylvania Ave., Washington, DC');
// the response should be an array of 0 or 1 addresses
// 0 if the address wasn't found, 1 if it was, and
// the geocoding info will be included
if (is_array($result) && count($result) > 0) {
$address = $result[0];
printf('%s %s %s<br/>', $address['number'], $address['street'], $address['type']);
printf('%s, %s %s<br/>', $address['city'], $address['state'], $address['zip']);
printf('at Latitude: %f, Longitude: %f', $address['lat'], $address['long']);
}
else {
print('Sorry, we could not find that address!');
}
}
}
catch (Exception $e) {
// We don't really care why the call failed, just tell the
// user that we couldn't get their results
print "Sorry, unable to look up your address at this time.";
}
?>
You can see in this example, calling a method with parameters is as simple as calling any other XML-RPC method. The data is passed in as regular PHP variables or literals, and the XML_RPC2_Client Class takes care of translating it to the XML datatypes.
Next: Creating an XML-RPC Server >>
More PEAR Articles Articles
More By Chris Moyer