Examples

Examples – Usage examples for the XML_RPC package

Using a Client to Get Info About the Latest PEAR Release

<?php
require_once 'XML/RPC.php';

/*
 * Get info about the most recently released PEAR package
 */
$params = array(new XML_RPC_Value(1'int'));
$msg = new XML_RPC_Message('release.getRecent'$params);

$cli = new XML_RPC_Client('/xmlrpc.php''pear.php.net');

// If you want to turn debugging on...
// $cli->setDebug(1);

// If your payload requires extra lines to stay in tact...
// NOTE: The $remove_extra_lines property was added in Version 1.4.6.
// $cli->remove_extra_lines = false;

// If inspect the XML request sent to the server...
// $msg->createPayload();
// logit($msg->payload);  // Hypothetical function.

$resp $cli->send($msg);

if (!
$resp) {
    echo 
'Communication error: ' $cli->errstr;
    exit;
}

if (!
$resp->faultCode()) {
    
$val $resp->value();
    
$data XML_RPC_decode($val);
    echo 
$data[0]['name'] . ' is at version ' $data[0]['version'];
} else {
    
/*
     * Display problems that have been gracefully cought and
     * reported by the xmlrpc.php script
     */
    
echo 'Fault Code: ' $resp->faultCode() . "\n";
    echo 
'Fault Reason: ' $resp->faultString() . "\n";
}

// To inspect the XML response from the server...
// NOTE: The $response_payload property was added in Version 1.4.6.
// logit($msg->response_payload);  // Hypothetical function.
?>

A Complete Client and Server Combination

Here is the server script. It's named xmlrpc.php and located in the document root of the web server at localhost:

<?php
require_once 'XML/RPC/Server.php';

/*
 * Declare the functions, etc.
 */
function returnTimes2($params) {
    
$obj = new some_class_name;
    return 
$obj->returnTimes2($params);
}

class 
some_class_name {
    function 
returnTimes2($params) {
        
$param $params->getParam(0);

        
// This error checking syntax was added in Release 1.3.0
        
if (!XML_RPC_Value::isValue($param)) {
            return 
$param;
        }

        
$val = new XML_RPC_Value($param->scalarval() * 2'int'); 
        return new 
XML_RPC_Response($val);
    }
}

$some_object = new some_class_name;


/*
 * Establish the dispatch map and XML_RPC server instance.
 */
$server = new XML_RPC_Server(
    array(
        
'function_times2' => array(
            
'function' => 'returnTimes2'
        
),
        
'class_paamayim_nekudotayim_times2' => array(
            
'function' => 'some_class_name::returnTimes2'
        
),
        
'class_times2' => array(
            
'function' => array('some_class_name''returnTimes2')
        ),
        
'object_times2' => array(
            
'function' => array($some_object'returnTimes2')
        ),
    ),
    
1  // serviceNow
);
?>

And here is the client script:

<?php
require_once 'XML/RPC.php';

$input 8;
$params = array(new XML_RPC_Value($input'int'));
$msg = new XML_RPC_Message('function_times2'$params);

$cli = new XML_RPC_Client('/xmlrpc.php''localhost');
// $cli->setDebug(1);
$resp $cli->send($msg);

if (!
$resp) {
    echo 
'Communication error: ' $cli->errstr;
    exit;
}

if (!
$resp->faultCode()) {
    
$val $resp->value();
    echo 
$input ' times 2 is ' $val->scalarval();
} else {
    
/*
     * Display problems that have been gracefully cought and
     * reported by the xmlrpc.php script.
     */
    
echo 'Fault Code: ' $resp->faultCode() . "\n";
    echo 
'Fault Reason: ' $resp->faultString() . "\n";
}
?>

Automatically encoding data

The XML_RPC_encode() function automatically converts PHP data into the format needed by the XML_RPC library.

<?php
require_once 'XML/RPC.php';

$data fetch_row_from_db();  //  Hypothetical example.

$params = array(XML_RPC_encode($data));
$msg = new XML_RPC_Message('some_function_name'$params);

$cli = new XML_RPC_Client('/xmlrpc.php''pear.php.net');
$resp $cli->send($msg);

// Process the same way as the other examples...
?>
Getting support for XML_RPC (Previous) Class documentation of the package (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.