XMLRPC for PHP - A simple client and server program

By: Emiley J  

Here is an example of using xmlrpc for PHP with a complete client and a server php files. You can use this to test your xml-rpc for php installations.

/* clienttest.php */ 
<?php 
function do_call($host, $port, $request) { 

$fp = fsockopen($host, $port, $errno, $errstr); 
$query = "POST /home/servertest.php HTTP/1.0\nUser_Agent: My Egg Client\nHost: ".$host."\nContent-Type: text/xml\nContent-Length: ".strlen($request)."\n\n".$request."\n"; 

if (!fputs($fp, $query, strlen($query))) { 
$errstr = "Write error"; 
return 0; 


$contents = ''; 
while (!feof($fp)) { 
$contents .= fgets($fp); 


fclose($fp); 
return $contents; 


$host = 'localhost'; 
$port = 80; 
$request = xmlrpc_encode_request('cycle', 'egg'); 
$response = do_call($host, $port, $request); 
/* do something with $response, e.g. print it */ 
?> 

/* servertest.php */ 
<?php 
function lifecycle($method, $params) { 
/* $method = 'cycle', $params = (array of) request parameter(s); $data is also passed from xmlrpc_server_call_method, if we had any data to pass */ 
switch($params[0]) { 
case 'egg': 
$reply = 'All eggs will be birds one day.'; 
break; 
default: 
$reply = 'That must have been an otheregg'; 

return $reply; 


$server = xmlrpc_server_create(); 

/* register the 'external' name and then the 'internal' name */ 
xmlrpc_server_register_method($server, "cycle", "lifecycle"); 

$request = $HTTP_RAW_POST_DATA; // no you don't need 'always on', and no $_POST doesn't work. 

/* the parameters here are 'server, xml-string and user data'. There's supposed to be an optional 'output options' array too, but I can't get it working :( hence header() call */ 
$response = xmlrpc_server_call_method($server, $request, null); 
header('Content-Type: text/xml'); 
print $response; 

xmlrpc_server_destroy($server); 
?>




Most Viewed Articles (in PHP )

Decrypting files using GnuPG (GPG) via PHP

A Tutorial on Timezones and Timestamps

Integers and Floating-Point Numbers in PHP

Installing PHP with nginx-server under windows

PHP 5.1.4 INSTALLATION on Solaris 9 (Sparc)

Building PHP 5.x with Apache2 on SuSE Professional 9.1/9.2

Installing PHP 5.x with Apache 2.x on HP UX 11i and configuring PHP 5.x with Oracle 9i

Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Function to return number of digits of an integer in PHP

Function to sort array by elements and count of element in PHP

Function to force strict boolean values in PHP

Function to convert strings to strict booleans in PHP

Latest Articles (in PHP)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. For everyone who didn't get this working: Make sur
View Tutorial          By: Hmail at 2011-02-13 16:33:47

2. when ever i run the client ..it gives a blank scre
View Tutorial          By: uday at 2012-09-03 12:39:47

3. Hello der. Found sumthng new for dis. Check it out
View Tutorial          By: Desmond Michael at 2016-02-10 06:53:14

4. Daniellot
View Tutorial          By: Daniellot at 2017-04-28 02:49:35

5. Shawncob
View Tutorial          By: Shawncob at 2017-04-28 03:39:19

6. Michaelped
View Tutorial          By: Michaelped at 2017-05-02 18:55:44

7. Derrickheili
View Tutorial          By: Derrickheili at 2017-05-08 08:56:24

8. EddieJeope
View Tutorial          By: EddieJeope at 2017-05-27 02:07:34

9. Donaldnob
View Tutorial          By: Donaldnob at 2017-06-06 22:28:55

10. Georgemes
View Tutorial          By: Georgemes at 2017-07-24 20:34:50

11. Larryhaw
View Tutorial          By: Larryhaw at 2017-07-24 20:46:31

12. RobertVeita
View Tutorial          By: RobertVeita at 2017-09-13 09:43:25