Article

Build your own Web Service with PHP and XML-RPC

Page: 1 2 3 4 5 6 7 Next

The XML-RPC Client

For the sake of example, we'll store the client script in the same place as the server, but if you have another Web server, you could place the script there, making sure it has access to "kd_xmlrpc.php", the functions library.

<?php      
/* Include the library */      
include ( "kd_xmlrpc.php" );      
     
/* Define variables to find the rpc server script */      
$site = "www.yourdomain.com";      
$location = "/kd_xmlrpc/server.php";      
     
/* Function to give us back a nice date */      
function convert_date ( $date ) {      
   $date = date ( "D M y H:i:s",      
                 XMLRPC_convert_iso8601_to_timestamp ( $date ) );      
   return ( $date );      
}      
?>      
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">      
<html>      
<head>      
<title> KD XML RPC News Client </title>      
<meta name="Generator" content="EditPlus">      
<meta name="Author" content="HarryF">      
<meta name="Keywords" content="XML RPC">      
<meta name="Description" content="Gets news form server.php">      
</head>      
<body>      
<?php      
     
/* client.php */      
/* If user is viewing a single news item, do this */      
if ( ISSET ( $_GET['news_id'] ) ) {      
     
   /* $success is 0 (fail) / 1 ( succeeded ).      
      XMLPRC_request preforms the XML POST to      
      the server script, calling the method and      
      sending the correct parameters using      
      XMLRPC_prepare */      
   list($success, $response) = XMLRPC_request(      
       $site,      
       $location,      
       'news.viewNewsItem',      
       array(XMLRPC_prepare($_GET['news_id']),      
       'HarryFsXMLRPCClient')      
   );      
     
   /* If all went well, show the article */      
   if ($success) {      
?>      
<table align="center" width="600">      
<tr valign="top">      
<th colspan="2"><b><?php echo ( $response['title'] );?></b></th>      
</tr>      
<tr valign="top">      
<th><?php echo ( $response['author'] );?></th>      
<th><?php echo ( convert_date ( $response['date'] ) );?></th>      
</tr>      
<tr valign="top">      
<td colspan="2">      
<?php echo ( nl2br ( $response['full_desc'] ) );?>      
</th>      
</tr>      
</table>      
<?php      
     
   /* Else display the error */      
   } else {      
       echo ( "<p>Error: " . nl2br ( $response['faultString'] ) );      
   }      
} else {      
     
   /* Define the parameters to pass to the XML-RPC      
   method as a PHP array */      
   $query_info['limit'] = 10;      
   $query_info['order'] = "author";      
     
   /* XMLRPC_prepare works on an array and      
   converts it to XML-RPC parameters */      
   list($success, $response) = XMLRPC_request(      
       $site,      
       $location,      
       'news.getNewsList',      
       array(XMLRPC_prepare($query_info),      
       'HarryFsXMLRPCClient')      
   );      
     
   /* On success, display the list as HTML table */      
   if ($success) {      
       echo ( "<table align=\"center\" width=\"600\">\n" );      
       $count = 0;      
       while ( list ( $key, $val ) = each ( $response ) ) {      
?>      
<tr valign="top">      
<td colspan="2">      
<a href="<?php echo ( $_SERVER['PHP_SELF'] );?>?news_id=<?php      
echo ( $response[$count]['news_id'] );      
?>">      
<?php echo ( $response[$count]['title'] ); ?>      
</a>      
</td>      
</tr>      
<tr valign="top">      
<td colspan="2">      
<?php echo ( $response[$count]['short_desc'] ); ?>      
</td>      
</tr>      
<tr valign="top">      
<td>      
<?php echo ( $response[$count]['author'] ); ?>      
</td>      
<td>      
<?php echo ( convert_date ( $response[$count]['date'] ) ); ?>      
</td>      
</tr>      
<?php      
           $count++;      
       }      
       echo ( "</table>\n" );      
     
   /* Or error */      
   } else {      
       echo ( "<p>Error: " . nl2br ( $response['faultString'] ) );      
   }      
}      
;?>      
</body>      
</html>

What happened here? The PHP code itself is simply an if/else structure, so that if we decide to look at a single news item using the $_GET['news_id'] variable, we display that item. Otherwise, we display the list of news items.

For the news list, we use the news.getNewsList method. Notice that we've set the $query_info['limit'] to 10 (i.e. we want 10 news items maximum) and $query_info['order'] is set to "author" (sort the news items by author name). These will become parameters that will be sent in the XML-RPC client request.

When we view a single news item, we'll turn the $_GET['news_id'] into a parameter, by which the server will be able to identify the news item we want.

The special XML-RPC functions we used here were:

  • XMLRPC_prepare(): We've used this before in the server, again to turn PHP variables into XML-RPC parameters. This is where we provide the server with the data it needs for the particular method we're using.
  • MLRPC_request(): This function accepts five variables and returns an array. It's basically what makes your client a client. Accepted are:
    • $site - the domain name of the server
    • $location - the path on the server from the Web root to the server script
    • $methodName - the name of the XML-RPC method we're invoking
    • $params - any variable required for the method
    • $user_agent - which is optional and can be anything you like (e.g. HarryFsXMLRPCClient ).

    The returned array contains two variables. The first is either a 0 for a failure, or a 1 for a successful response to the request, which we use for error checking. The second is a multidimensional array that contains all the data from the response.

  • -XMLRPC_convert_iso8601_to_timestamp: with our convert_date() function at the top, we use this special function to change the XML-RPC date format into something that looks nice, via an intermediate PHP timestamp.

One tip: for writing XML-RPC clients, it can be helpful to think of the server in the same way that you'd think of a database -- it's just a source of data. In terms of your scripts, the code will be a similar structure to that which you'd use when querying MySQL.

So that's it! Point your browser at http://www.yourdomain.com/kd_xmlrpc/client.php, sit back and read the news.

Download the Code: the scripts used in this tutorial and the MySQL table are all in this zip file.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links