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); 
?>



Archived Comments

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

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

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

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

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

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

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

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

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

10. Hello der. Found sumthng new for dis. Check it out.
http://vaaiibhav.me/building-php-xml-rpc-

View Tutorial          By: Desmond Michael at 2016-02-10 06:53:14

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

12. For everyone who didn't get this working: Make sure there are (obviously) no tabs or unnecessary new
View Tutorial          By: Hmail at 2011-02-13 16:33:47


Most Viewed Articles (in PHP )

PHP convert string to lower case

Input Validation in PHP

preg_split() and explode() in PHP

Reading word by word from a file in PHP

Using Text-File Databases in PHP

Generate random timestamp between two dates

Error: Length parameter must be greater than 0

PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......

Appending One Array to Another in PHP

Installing PHP with nginx-server under windows

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

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

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

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

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

Latest Articles (in PHP)

Comment on this tutorial