PEAR Articles
  Home arrow PEAR Articles arrow Page 2 - Using XML_RPC2 with PEAR
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PEAR ARTICLES

Using XML_RPC2 with PEAR
By: Chris Moyer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-07-30

    Table of Contents:
  • Using XML_RPC2 with PEAR
  • Creating an XML-RPC Client
  • Creating an XML-RPC Server
  • Cached Results

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PEAR Articles Articles
    More By Chris Moyer


     

    PEAR ARTICLES ARTICLES

    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning
    - Using XML_RPC2 with PEAR
    - Using Web Service APIs (Amazon and Yahoo!) w...
    - Database Abstraction with MDB2 from PEAR





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek