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
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
PHP convert string to lower case
PHP code to write to a CSV file for Microsoft Applications
PHP code to write to a CSV file from MySQL query
PHP code to import from CSV file to MySQL
Password must include both numeric and alphabetic characters - Magento
Error: Length parameter must be greater than 0
PHP file upload prompts authentication for anonymous users
PHP file upload with IIS on windows XP/2000 etc
Multiple File Upload in PHP using IFRAME
Resume or Pause File Uploads in PHP
Exception in module wampmanager.exe at 000F15A0 in Windows 8