有没有可能通过php检查WordPress是否启用了XML-RPC?类似于编写一个函数来测试这一点。
if(is_xmlrpc_enabled()) {
//action
}
else {
//another action
}
默认情况下,WordPress版本大于3.5的XML-RPC功能是启用的(使用“xmlrpc_enabled”钩子可以禁用它)。
对于旧版本,数据库中有一个字段(选项表),指示它是否启用。(这个选项在WP > 3.5中已被删除)
function is_xmlrpc_enabled() {
$returnBool = false;
$enabled = get_option('enable_xmlrpc'); //for ver<3.5
if($enabled) {
$returnBool = true;
}
else {
global $wp_version;
if (version_compare($wp_version, '3.5', '>=')) {
$returnBool = true; //its on by default for versions above 3.5
}
else {
$returnBool = false;
}
}
return $returnBool;
}
WordPress的XML-RPC服务器有两种测试方法:
demo.sayHello – Returns a standard “Hello!” message.
demo.addTwoNumbers – Accepts an array containing two numbers and returns the sum.
function sayHello()
{
$params = array();
return $this->send_request('demo.sayHello',$params);
}
$objXMLRPClientWordPress = new XMLRPClientWordPress("http://localhost/wordpress31/xmlrpc.php" , "username" , "passowrd");
function send_request($requestname, $params)
{
$request = xmlrpc_encode_request($requestname, $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
如果您获得相同的结果,这意味着您能够正确地向WordPress XML-RPC服务器发送请求并正确接收请求。