Miscellaneous

  Home arrow Miscellaneous arrow Page 5 - Remote Procedure Calls with PEAR::XML-...
MISCELLANEOUS

Remote Procedure Calls with PEAR::XML-RPC
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2004-01-29

    Table of Contents:
  • Remote Procedure Calls with PEAR::XML-RPC
  • The Example Script
  • An XML_RPC Exchange
  • Client Rewritten Example
  • Client Sending
  • Client Receiving
  • Server Rewritten Example
  • Distributed Function Requirements
  • Inside the Function
  • Map and Send
  • The Output
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Remote Procedure Calls with PEAR::XML-RPC - Client Sending


    (Page 5 of 12 )

    We start by requiring the use the PEAR package XML/RPC.php. The $first and $last variables remain unchanged, but we store the name of the remote function as $function. The $number variable will serve the same purpose as before but will appear later in our script.

    <?php
    require_once("XML/RPC.php");

    $first "George";
    $last "Washington";

    $function "getNameLength";
    ?>

    The XML-RPC specification was designed for use by a variety of languages; while PHP is a loosely typed language we are still required to pass the value's type. Valid data types are: int, double, string, boolean, array, struct, dateTime.iso8601 and base64.

    We define each parameter to be passed using XML_RPC_Value that accepts a value and it's type. The $params array simply gathers the resulting objects into an array.

    <?php
      $p1 
    = new XML_RPC_Value($first"string");
      
    $p2 = new XML_RPC_Value($last"string");
      
    $params = array($p1$p2);
    ?>

    An XML message object is constructed by passing the function name and the array of parameters to XML_RPC_Message.

    <?php
      $message 
    = new XML_RPC_Message($function$params);
    ?>

    In order to send the message we must be able to locate the remote server. A connection is established by using XML_RPC_Client and passing the server interface, the remote server's address and the designated port number through which communications will travel. Again, the most common transport mechanism used for RPC is the HTTP protocol and so we use port 80.

    Finally, the message is sent by invoking the send method on the server object. The method accepts the message object and returns an XML response object which we store as $response.

    <?php
    $client 
    = new XML_RPC_Client("/RPC2.php""localhost"80);
    $result $client-&gt;send($message);
    ?>

    The serialize method can be used at any time on either a message or response object to view the data in context of its XML wrappings.

    <?php
      
    echo "&lt;p&gt;&lt;b&gt;Function Call:&lt;/b&gt; $function($first, $last)&lt;/p&gt;";
      echo 
    "&lt;p&gt;&lt;b&gt;Sent XML Message:&lt;/b&gt;";
      echo 
    "&lt;pre&gt;" htmlentities($message-&gt;serialize()). "&lt;/pre&gt;&lt;/p&gt;";
    ?>

    More Miscellaneous Articles
    More By bluephoenix

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap