XML-RPC
XML-RPC and SOAP are two of the standard protocols used to create web services. XML-RPC is the older (and simpler) of the two, while SOAP is newer and more complex. Microsoft’s .NET initiative is based on SOAP, while many of the popular web journal packages, such as Frontier and Blogger, offer XML-RPC interfaces.
PHP provides access to both SOAP and XML-RPC through the
xmlrpc extension, which is based on the
xmlrpc-epi project (see http://xmlrpc-epi.sourceforge.net for more information).
The xmlrpc extension is not compiled in by default,
so you’ll need to add --with-xmlrpc
to your configure
line when you compile PHP.
Servers
Example 15-7 shows a very basic XML-RPC server that exposes only one
function (which XML-RPC calls a “method”). That function, multiply()
, multiplies two numbers and returns
the result. It’s not a very exciting example, but it shows the basic
structure of an XML-RPC server.
Example 15-7. Multiplier XML-RPC server
<?
php
// expose this function via RPC as "multiply()"
function
times
(
$method
,
$args
)
{
return
$args
[
0
]
*
$args
[
1
];
}
$request
=
$HTTP_RAW_POST_DATA
;
if
(
!
$request
)
{
$requestXml
=
$_POST
[
'xml'
];
}
$server
=
xmlrpc_server_create
()
or
die
(
"Couldn't create server"
);
xmlrpc_server_register_method
(
$server
,
"multiply"
,
"times"
);
$options
=
array
(
'output_type'
=>
'xml'
,
'version'
=>
'auto'
);
echo
xmlrpc_server_call_method
(
$server
,
$request
,
null
,
$options
);
xmlrpc_server_destroy
(
$server
);
The xmlrpc extension handles the dispatch for you. That is, it ...
Get Programming PHP, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.