PEAR Manual
Prev |  | Next

# Examples

Examples -- Usage examples for the XML_RPC package

## Using a Client to Get Info About the Latest PEAR Release

Example 60-1.
    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');
    // $cli->setDebug(1);
    $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";
    }

## A Complete Client and Server Combination

Example 60-2. Here is the server script. It's named xmlrpc.php and located in the document root of the web server at localhost:
    require_once 'XML/RPC/Server.php';

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

    $server = new XML_RPC_Server(
        array(
            'times2' =>
                array(
                    'function' => 'returnTimes2'
                )
        )
    );
And here is the client script:
    require_once 'XML/RPC.php';

    $input = 8;
    $params = array(new XML_RPC_Value($input, 'int'));
    $msg = new XML_RPC_Message('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";
    }

Prev | Home | Next
Support | Up | API