I’m working on an integration between an outside app (which needs to pull user names and some taxonomy data, and create new posts) and a WordPress site. I’m running the latest build of WP (4.0).
The XML-RPC documentation being as spare as it is, I’m hoping someone out there can just pastebin a recent (2014?) working example. Honestly, the google machine simply fails on this one.
Ideally, the code example would use the WP’s bundled libraries (class-IXR.php, class-wp-http-ixr-client.php) rather than some outside XMLRPC library.
Here’s what’s NOT working (yet):
<?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); ?>
How complex do you want the example to be?
This outputs “Hello”.
$client = new WP_HTTP_IXR_Client('http://example.com/xmlrpc.php'); $client->query('demo.sayHello'); echo $client->getResponse();
This outputs “9”.
$client = new WP_HTTP_IXR_Client('http://example.com/xmlrpc.php'); $client->query('demo.addTwoNumbers', 4, 5); echo $client->getResponse();
This gets the WordPress version:
$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'];
Source: Me, 4 years ago:
Okay, I was being stupid, so I stopped that.
I’d assumed that a) any answer older than two years would be out-of-date; b) I’d need a special WP flavor of the base IXR library; I was confused, and took the sparseness of the wp.org documentation as “they’re not telling the whole story” instead of “the tool is very basic at the use interface.”
This code works, in conjunction with the IXR library found at 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);