PHP
  Home arrow PHP arrow Page 6 - Using XML-RPC with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
PHP

Using XML-RPC with PHP
By: Lucas Marshall
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 33
    2001-12-20


    Table of Contents:
  • Using XML-RPC with PHP
  • Compiling PHP with XML-RPC Support
  • Dissection of a XML-RPC Call
  • Dissection of a XML-RPC Response
  • Creating an XML-RPC Server
  • Creating an XML-RPC Client
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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-RPC with PHP - Creating an XML-RPC Client
    ( Page 6 of 7 )

    Now that we have a server, we need a client to call our methods. As mentioned before, the XML-RPC EPI extension for PHP does not make HTTP requests by itself. For that reason, before it was included in the PHP distribution, the XML-RPC EPI PHP extension came with some include files that had functions that make it a lot easier to work with XML-RPC requests.

    The only way to get these functions is to download the XML-RPC EPI PHP extension source package from http://xmlrpc-epi.sourceforge.net/, and install the files in the /sample/utils directory into a directory in your PHP include_path. For the purpose of the next pieces of example code, I will assume that you have copied the entire utils directory into a directory in your include_path, and renamed it xmlrpcutils.

    Making a client is fairly simple. The function that does all the work when we call our methods is xu_rpc_http_concise(), which is defined in the utils.php file of the xmlrpcutils directory now in our PHP include_path.

    First we'll make a client that calls the first method we defined in the server section of the article, the uptime method. This method does not require any parameters, so we won't pass any in. My comments about what we are doing and why will appear as comments in the code.

    <?php /* * This allows us to use the functions that make * making XML-RPC requests easy. */ include("xmlrpcutils/utils.php"); /* * For the next two lines use this guide. * If your server script can be found at the URL: * http://myhost.mydomain.com/xmlrpc_server.php * $host should be "myhost.mydomain.com" * and * $uri should be "/xmlrpc_server.php" */ // Change these to match your configuration $host = "myhost.mydomain.com"; $uri = "/xmlrpc_server.php"; /* * Here's where we make the request. xu_rpc_http_concise() * takes one parameter - a keyed array that specifies all * the info needed for the request. * The most important (and required) elements are: * 'method' - specifies the method to call * 'host' - specifies the host the server script is on * 'uri' - specifies the uri of the server script on the server * 'port' - specifies the port the server is listening on * * Here we are calling the uptime method of the * server script who's uri is specified in $uri of the * server that is at the hostname specified in $host and * listening on port 80 and storing it's response (converted * to a PHP data type) in $result. */ $result = xu_rpc_http_concise( array( 'method' => "uptime", 'host' => $host, 'uri' => $uri, 'port' => 80 ) ); print "The uptime on " . $host . " is " . $result; ?>
    When this is run it should print something like:
    The uptime on myhost.mydomain.com is 9:43pm up 5:12, 2 users, load average: 0.25, 0.24, 0.22
    Where the uptime numbers are the same as those you get if you run the 'uptime' command on the server running the server script.

    Our second example will call the greeting method. This is one that requires a parameter - a name.

    <?php include("xmlrpcutils/utils.php"); $host = "myhost.mydomain.com"; $uri = "/xmlrpc_server.php"; // Change this to your name $name = "yourname"; /* * The difference between this call and the last is we pass * in an array as the 'args' element of the array passed as * the argument of the xu_rpc_http_concise() function. */ $result = xu_rpc_http_concise( array( 'method' => "greeting", 'args' => array($name), 'host' => $host, 'uri' => $uri, 'port' => 80 ) ); print $result; ?>
    When run, this should print:

    Hello, yourname. How are you today?
    These are some very simple examples - xu_rpc_http_concise() can take some other arguments as well, and there are more functions available. I recommend that you read through the files in the xmlrpcutils directory to learn these for yourself.

     
     
    >>> More PHP Articles          >>> More By Lucas Marshall
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT