这是我第一次使用 XML-RPC。我在从服务器检索数据时遇到问题,因为它作为故障代码返回给客户端。为什么会这样?我还需要制作一个 XML-RPC 服务器文件还是已经在这里设置了?
$beerClient = new xmlrpc_client('localhost:1234/341/xmlrpc-lab/xmlrpcserver.php','alvin.ist.rit.edu:8100',1234);
是因为数据与服务器返回类型不匹配还是我缺少的其他东西?
我确保将 xmlrpc 库包含在正确的路径和所有内容中。
网页错误返回:
An XML-RPC Fault Occured
Fault Code:-1
Fault Desc:java.lang.NoSuchMethodException: BeerHandler.List()
XML-RPC 客户端:
<?php
require_once('xmlrpc-3.0.0.beta/lib/xmlrpc.inc');
// Initialize $Beers as an empty Array.
// $Beers will hold all the information available for each beer.
// (BeerID, Name, Rating, Comments)
//
// It will be used later to create the HTML output.
$Beers = Array();
// GET A LIST OF THE AVAILABLE BEERS ON THE XML-RPC SERVER
$beerClient = new xmlrpc_client('localhost:1234/341/xmlrpc-lab/xmlrpcserver.php','alvin.ist.rit.edu:8100',1234);
$msg_listBeers = new xmlrpcmsg('beer.List');
$response = $beerClient->send($msg_listBeers);
if($response == false)
{
die('Unable to contact XML-RPC Server');
}
if(!$response->faultCode()) // faults occurred?
{
// convert to a more usable PHP Array
$beerList = xmlrpc_decode($response->value());
foreach($beerList as $beerID => $beerName)
{
// for each available beer listed by the server grab it's
// a) Rating
// b) Comments
//
// and put the data into the $Beer array. This is incredibly
// inefficient since it performs a lot of HTTP calls to grab each
// piece of data. Not recommended for use beyond this example.
$Beers[$beerID]['name'] = $beerName;
// GET THE RATING FOR THE CURRENT BEER
// Build XML-RPC Request for the Beer's Rating
$msg_beerRating = new xmlrpcmsg('beer.Rating',array(new xmlrpcval($beerID, 'int')));
// Send the Message to the server
$response = $beerClient->send($msg_beerRating);
// Negative number If no rating found
$Beers[$beerID]['rating'] = (!$response->faultCode()) ? xmlrpc_decode($response->value()) : -1;
// GET THE COMMENTS FOR THE CURRENT BEER
$msg_beerComments = new xmlrpcmsg('beer.Comments', array(new xmlrpcval($beerID,'int')));
// Send the Message to the server
$response = $beerClient->send($msg_beerComments);
// Fill in the Comments for the Beer
$Beers[$beerID]['comments'] = (!$response->faultCode()) ? xmlrpc_decode($response->value()) : array(); //empty array for not Comments
} // END foreach ($beerList as ...
}
else // XML-RPC returned a fault...
{
echo '<h1>An XML-RPC Fault Occured</h1>';
printf('<b>Fault Code:</b>%s<br/>',$response->faultCode());
printf('<b>Fault Desc:</b>%s<br/>',$response->faultString());
die();
}
// GENERATE THE OUTPUT
$xmlrpc_client->setDebug(1); // turn on debugging, if I/O fault occurred between communication xmlrpc_client and xmlrpc_server
?>
<html>
<head>
<title>Beer List XML-RPC</title>
<meta charset="utf-8">
</head>
<body>
<h1>Beer List</h1>
<ol type="1">
<?php
if(count($Beers))
{
foreach($Beers as $BeerData)
{
printf('<font size="+2" color="#990000"><li> %s</font><br>', $BeerData['beer']);
// Output the beer's rating if it exists
if($BeerData['rating'] != -1)
{
printf('<b>Rating: %s/5.0</b><br>',$BeerData['rating']);
}
// Output the beer's comments if they exist
if(count($BeerData['comments']))
{
echo '<b>Comments:</b><ul>';
foreach($BeerData['comments'] as $Comment)
{
echo "<li> $Comment";
}
echo '</ul>';
}
}
}
else
{
echo '<li> Darn No Beers Found';
}
?>
</ol>
</body>
</html>