我正在研究一个外部应用程序(需要提取用户名和一些分类数据,并创建新帖子)和WordPress站点之间的集成。我正在运行最新版本的WP(4.0)。
XML-RPC文档尽可能多,我希望有人可以粘贴最近的(2014?)工作示例。老实说,谷歌机器在这一点上完全失败了。
理想情况下,代码示例将使用WP的捆绑库(class-IXR.php,class-WP-http-IXR-client.php),而不是使用XMLRPC库之外的一些库。
以下是(目前)不起作用的内容:
<?php
get_header();
include_once( ABSPATH . WPINC . \'/class-IXR.php\' );
include_once( ABSPATH . WPINC . \'/class-wp-http-ixr-client.php\' );
$client = new WP_HTTP_IXR_CLIENT( \'redactedSITEURL\' );
$post = array(
\'post_type\' => \'post\',
\'post_status\' => \'draft\',
\'post_title\' => \'Test Post\',
\'post_content\' => \'This is my test post\',
\'post_author\' => 1
);
$data = xmlrpc_encode_request(\'wp.newPost\', array(\'redactedURL.com\', \'redactedUNAME\', \'redactedPASSWORD\', $post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, \'redactedSITEURL.com/xmlrpc.php\');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$exec = curl_exec($ch);
$response = xmlrpc_decode($exec);
curl_close($ch);
var_dump($response);
?>
最合适的回答,由SO网友:Benjamin Barnett 整理而成
好吧,我太傻了,所以我停止了。
我认为a)任何超过两年的答案都将过时;b) 我需要一个特殊的WP风格的基本IXR库;我很困惑,认为wp是稀疏的。将文档组织为“他们没有讲述全部情况”,而不是“工具在使用界面上非常基本”
此代码与位于incutio.com:
include(\'IXR_Library.php\');
$usr = \'theusername\';
$pwd = \'thepassword\';
$xmlrpc = \'http://not-therealurl.com/xmlrpc.php\';
$client = new IXR_Client($xmlrpc);
$client -> debug = true; //optional but useful
$params = array(
\'post_type\' => \'post\',
\'post_status\' => \'draft\',
\'post_title\' => \'Test Post\',
\'post_author\' => 4,
\'post_excerpt\' => \'This is my test excerpt\',
\'post_content\' => \'This is my test post. Now its longer than the excerpt.\'
);
$res = $client -> query(\'wp.newPost\',1, $usr, $pwd, $params);
SO网友:Otto
您希望示例有多复杂?
这将输出“Hello”。
$client = new WP_HTTP_IXR_Client(\'http://example.com/xmlrpc.php\');
$client->query(\'demo.sayHello\');
echo $client->getResponse();
输出“9”。
$client = new WP_HTTP_IXR_Client(\'http://example.com/xmlrpc.php\');
$client->query(\'demo.addTwoNumbers\', 4, 5);
echo $client->getResponse();
这将获取WordPress版本:
$client = new WP_HTTP_IXR_Client(\'http://example.com/xmlrpc.php\');
$client->query(\'wp.getOptions\', 0, \'username\', \'password\', \'software_version\');
$response = $client->getResponse();
echo $response[\'software_version\'][\'value\'];
资料来源:我,4年前:
http://ottopress.com/2010/wordpress-3-1-and-xml-rpc/