Article
Build your own Web Service with PHP and XML-RPC
Preparations
1. Download the code archive for this article -- it'll save you from laboriously cutting and pasting the code from the pages of this tutorial.
2. Download the source code from http://www.keithdevens.com/software/xmlrpc/source.php and save it as "kd_xmlrpc.php".
3. Now create a directory on your Web server called kd_xmlrpc, below your Web root (where your "home page" lives), and place kd_xmlrpc.php there. We'll be putting all the other files we create below in the same directory.
4. Create a table in your MySQL database using this query:
CREATE TABLE kd_xmlrpc_news (
news_id mediumint(9) NOT NULL auto_increment,
title varchar(255) default NULL,
short_desc text,
full_desc text,
author varchar(100) default NULL,
date datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (news_id)
) TYPE=MyISAM COMMENT='XML RPC News';
4. Now INSERT some dummy news items into the table -- or, if you're lacking inspiration, run the "kd_xmlrpc_news.sql" query you'll find in the code archive (downloaded in step 1).
The XML-RPC Server
The database is now ready for action. Next, let's create the XML-RPC server, and call it "server.php":
<?php
/* server.php */
/* Variables for accessing MySQL */
$dbserver= "localhost"; // Hostname of your MySQL server
$db = "database_name"; // Name of your MySQL database
$dbuser = "username"; // MySQL user with access to $db
$dbpassword = "password"; // Password for MySQL user
/* Connect to the MySQL server */
$link = @mysql_connect ($dbserver, $dbuser, $dbpassword);
if (! $link){
echo ( "Unable to connect to db" );
exit();
}
/* Select the database */
if (!mysql_select_db ($db, $link) ){
exit ();
}
/* Include Keith's xml-rpc library */
include("kd_xmlrpc.php");
/* Include a file that defines all the xml-rpc "methods" */
include("web_service_api.php");
/* Now use the XMLRPC_parse function to take POST
data from what xml-rpc client connects and turn
it into normal PHP variables */
$xmlrpc_request = XMLRPC_parse($GLOBALS['HTTP_RAW_POST_DATA']);
/* From the PHP variables generated, let's get the
method name ie. server asks "What would you like
me to do for you?" */
$methodName = XMLRPC_getMethodName($xmlrpc_request);
/* Get the parameters associated with that method
e.g "So you want to view a news item. Tell me
which one you want. What's the id#?" */
$params = XMLRPC_getParams($xmlrpc_request);
/* Error check - if a method was used that doesn't
exist, return the error response to the client */
if(!isset($xmlrpc_methods[$methodName])){
$xmlrpc_methods['method_not_found']($methodName);
/* Otherwise, let's run the PHP function corresponding
to that method - note the functions themselves
return the correct formatted xml-rpc response
to the client */
}else{
/* Call the method - notice $params[0] not just $params as the
documentation states. */
$xmlrpc_methods[$methodName]($params[0]);
}
?>
Here's a quick summary of what happens within server.php:
- Connect to MySQL to give the script access to the database.
- Include the kd_xmlrpc.php code so we can use Keith's functions.
- Include web_service_api.php, which defines the XML-RPC methods (see below).
- Use the
XMLRPC_parse()function to take the contents of$GLOBALS['HTTP_RAW_POST_DATA'], which contains the request from an XML-RPC client, and convert the XML into PHP variables. - Now determine which XML-RPC method has been called, using the
XMLRPC_getMethodName()function. We want the same XML-RPC server to be capable of more than one task (method/function), and here's where we determine which one we'll carry out for the current request (e.g "news.getNewsList" or "news.viewNewsItem"). - Now check for any parameters sent in the client request, such as an "
id" (identifying number) of a news item. - Finally we check that the method exists, as defined in web_service_api.php, and if everything looks good, we run th method (i.e. PHP function) required. If we can't find the method, we run the default "
method_not_found" method.
The special XML-RPC functions we used were:
XMLRPC_parse(): this function takes some XML and turns it into PHP variables -- so the XML from the client's request is converted to a form that PHP can work with. At this point, this isn't specific to the XML-RPC standard -- any XML that's sent will be converted to PHP variables.XMLRPC_getMethodName()determine which method is being used so we can decide which PHP function to use.XMLRPC_getParams()takes any XML-RPC parameters and converts them to PHP variables.
That's basically the XML-RPC server finished. In general, this is what you'd do for every XML-RPC server you build. Not hard, was it? Now all we need are those PHP functions to be used with the methods...