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.
Next: Conclusion >>
More PHP Articles
More By Lucas Marshall