10. PHP·Î XML-RPC »ç¿ëÇϱâ

Edd Dumbill´Â PHP¿ë XML-RPC ¸ðµâÀ» ¸¸µé¾ú´Ù. ¸ðµâÀº UsefulInc XML-RPC website¿¡¼­ ±¸ÇÒ ¼ö ÀÖ´Ù.

ÆÄÀÏÀ» ³»·Á¹Þ¾Æ ¾ÐÃàÀ» Ǭ ´ÙÀ½, xmlrpc.inc¿Í xmlrpcs.inc ÀÇ µÎ ÆÄÀÏÀ» PHP ½ºÅ©¸³Æ®¿Í °°Àº µð·ºÅ丮¿¡ º¹»çÇÏ¸é µÈ´Ù.

10.1. PHP Ŭ¶óÀ̾ðÆ®

´ÙÀ½ÀÇ ½ºÅ©¸³Æ®´Â À¥ÆäÀÌÁö¿¡ XML-RPC È£Ãâ ·çÆ¾À» Ãß°¡ÇÏ´Â ¹æ¹ýÀ» º¸¿©ÁØ´Ù.

<html>
<head>
<title>XML-RPC PHP Demo</title>
</head>
<body>
<h1>XML-RPC PHP Demo</h1>

<?php
include 'xmlrpc.inc';

// Make an object to represent our server.
$server = new xmlrpc_client('/api/sample.php',
                            'xmlrpc-c.sourceforge.net', 80);

// Send a message to the server.
$message = new xmlrpcmsg('sample.sumAndDifference',
                         array(new xmlrpcval(5, 'int'),
                               new xmlrpcval(3, 'int')));
$result = $server->send($message);

// Process the response.
if (!$result) {
    print "<p>Could not connect to HTTP server.</p>";
} elseif ($result->faultCode()) {
    print "<p>XML-RPC Fault #" . $result->faultCode() . ": " .
        $result->faultString();
} else {
    $struct = $result->value();
    $sumval = $struct->structmem('sum');
    $sum = $sumval->scalarval();
    $differenceval = $struct->structmem('difference');
    $difference = $differenceval->scalarval();
    print "<p>Sum: " . htmlentities($sum) .
        ", Difference: " . htmlentities($difference) . "</p>";
}
?>

</body></html>

À¥¼­¹ö¿¡¼­ PHP ½ºÅ©¸³Æ®°¡ ½ÇÇàµÇÁö ¾Ê´Â´Ù¸é PHP À¥»çÀÌÆ®¸¦ ÂüÁ¶Ç϶ó.

10.2. PHP ¼­¹ö

´ÙÀ½ÀÇ ½ºÅ©¸³Æ®´Â PHP¸¦ ÀÌ¿ëÇÏ¿© XML-RPC ¼­¹ö¿¡ Àû¿ëÇÏ´Â ¹æ¹ýÀ» º¸¿©ÁØ´Ù.

<?php
include 'xmlrpc.inc';
include 'xmlrpcs.inc';

function sumAndDifference ($params) {

    // Parse our parameters.
    $xval = $params->getParam(0);
    $x = $xval->scalarval();
    $yval = $params->getParam(1);
    $y = $yval->scalarval();

    // Build our response.
    $struct = array('sum' => new xmlrpcval($x + $y, 'int'),
                    'difference' => new xmlrpcval($x - $y, 'int'));
    return new xmlrpcresp(new xmlrpcval($struct, 'struct'));
}

// Declare our signature and provide some documentation.
// (The PHP server supports remote introspection. Nifty!)
$sumAndDifference_sig = array(array('struct', 'int', 'int'));
$sumAndDifference_doc = 'Add and subtract two numbers';

new xmlrpc_server(array('sample.sumAndDifference' =>
                        array('function' => 'sumAndDifference',
                              'signature' => $sumAndDifference_sig,
                              'docstring' => $sumAndDifference_doc)));
?>

À§ÀÇ ½ºÅ©¸³Æ®¸¦ À¥¼­¹ö¿¡¼­ http://localhost/path/sumAndDifference.php¿Í °°Àº ¹æ½ÄÀ¸·Î ½ÇÇà½Ãų ¼ö ÀÖ´Ù.