在标头中传递参数(XML RPC)

时间:2010-03-16 21:39:50

标签: php parameters curl xml-rpc

我正在尝试为MMORPG Champions Online设置服务器状态。我从网站管理员那里得到了一些基本信息,这就是他告诉我的所有信息:

现在,我找到了一个很好的例子,从here开始,我最终得到了这段代码:

<?php
 ini_set('display_errors', 1);
 error_reporting(E_ALL);

# Using the XML-RPC extension to format the XML package
$request = xmlrpc_encode_request("wgsLauncher.getServerStatus", "<param><value><string>en-US</string></value></param>", null );

# Using the cURL extension to send it off, 
# first creating a custom header block
$header[] = "Host: http://www.champions-online.com:80/";
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($request) . "\r\n";
$header[] = $request;

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://www.champions-online.com/xmlrpc.php"); # URL to post to
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header ); # custom headers, see above
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); # This POST is special, and uses its specified Content-type
$result = curl_exec( $ch ); # run!
curl_close($ch); 

echo $result;
?>

但是我收到了“400 Bad Request”错误。我是XML RPC的新手,我几乎不知道php,所以我很茫然。来自php网站的examples显示如何使用数组作为参数,但没有别的。

我从这个XMLRPC Debugger获得了参数字符串<param><value><string>en-US</string></value></param>(非常好的顺便说一句)。我在“有效载荷”框中输入了我需要的参数,这是输出。

因此,如果将此参数传递给标题,我将不胜感激。

注意:我的主机支持xmlrpc但似乎功能“xmlrpc_client”不存在。


更新:网站管理员回复了这些信息,但它仍然无法正常工作......我可能只是抓住了页面上的状态。

$request = xmlrpc_encode_request("wgsLauncher.getServerStatus", "en-US" );

1 个答案:

答案 0 :(得分:1)

好的,我终于找到了答案......这似乎是标题中的一个问题,因为当我更改cURL代码以匹配我在this site上找到的代码时它起作用了。这篇文章是关于如何使用php中的XMLRPC远程发布到wordpress。

这是我最终得到的代码:

<?php

// ini_set('display_errors', 1);
// error_reporting(E_ALL);

# Using the XML-RPC extension to format the XML package
$request = xmlrpc_encode_request( "wgsLauncher.getServerStatus", "en-US" );

$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, "http://www.champions-online.com/xmlrpc.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$result = curl_exec($ch);
curl_close($ch);

$method = null;
$params = xmlrpc_decode_request($result, &$method); 

# server status result = true (up) or false (down)
$status = ($params['status']) ? 'up' : 'down';
$notice = ($params['notice'] == "") ? "" : "Notice: " + $params['notice'];
echo "Server Status: " . $status . "<br>";
echo $notice;

?>