Support (Previous) (Next) API

View this page in Last updated: Sun, 28 Sep 2008
English | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Plain HTML

Examples

Examples -- Usage examples for the XML_RPC package

Using a Client to Get Info About the Latest PEAR Release

Example 67-1.


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

Example 67-2.

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";
}
?>

Support (Previous) (Next) API

Download Documentation Last updated: Sun, 28 Sep 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
There are no user contributed notes for this page.