p2p with wordpress xml-rpc

Simple fun with WordPress xml-rpc. I want a way to return a welcome message to interested blog owners.

To get that done, I make a new plugin that adds a new method to the xml-rpc server.

The method ‘p2p.welcome’ is mapped to the plugin function ‘p2p_welcome’, which returns the “about yourself” description of the admin profile.

/*
Plugin Name: P2P rpc
Description: RPC Peer to Peer stuff
Version: 1.0.0  
Author: Juust
Author URI: https://www.juust.org/
*/

/* xmlrpc_methods hooks into IXR_Server */    
add_filter( 'xmlrpc_methods', 'p2p_xmlrpc_methods' );

/* pass the $methods array and add a new one */    
function p2p_xmlrpc_methods( $methods ) {
	    $methods['p2p.whoami'] = 'p2p_whoami';
	    return $methods;
}

/* the function that returns the profile description of user 1 (admin) */    
function p2p_whoami() {
	return get_the_author_meta( 'user_description', 1 );
}

I plug that into the wordpress blog.

Then I can make a small test program :

  • include class-IXR.php which has the IXR_Client class
  • extend IXR_Client as P2PClient
  • add a function P2PClient->whoareyou()
  • …that calls the new method p2p.whoami on the blog
  • echo the returned data : the admin profile description
include_once('/absolute/path/to/class-IXR.php');

Class P2PClient extends IXR_Client { 

var $MyPeerConnection;
var $response;
var $url;

function __construct($url="") {
                 $this->url = $url; 
		  if(!$this->connect($url)) return false;
	    }
	    
//see if there is an xml rpc endpoint
function connect($url)
	    {
		     if(!$this->MyPeerConnection = new IXR_Client($this->url)) return false;
	    }

 //query 'p2p.whoami', store the response
function whoareyou() {
		$this->MyPeerConnection->query('p2p.whoami');
		$this->response = $this->MyPeerConnection->getResponse();
	    }
}

//init the client, query, echo response
$p2p = new P2PClient('http://loaneys.com/xmlrpc.php');
$p2p->whoareyou();
echo "resp ". $p2p->response;

That returns :

Hi I am juust, admin of loaneys.com. I worked as business economist at project control and proces automation in engineering and utilities. These days, I do some scripting as a hobby and stick to painting as job.

That is a basic way to exchange blog data by extending the xml-rpc endpoint.

Posted in wordpress, xml-rpc.

Leave a Reply

Your email address will not be published. Required fields are marked *