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.

  1. /*
  2. Plugin Name: P2P rpc
  3. Description: RPC Peer to Peer stuff
  4. Version: 1.0.0  
  5. Author: Juust
  6. Author URI: http://www.juust.org/
  7. */
  8.  
  9. /* xmlrpc_methods hooks into IXR_Server */    
  10. add_filter( 'xmlrpc_methods', 'p2p_xmlrpc_methods' );
  11.  
  12. /* pass the $methods array and add a new one */    
  13. function p2p_xmlrpc_methods( $methods ) {
  14.      $methods['p2p.whoami'] = 'p2p_whoami';
  15.      return $methods;
  16. }
  17.  
  18. /* the function that returns the profile description of user 1 (admin) */    
  19. function p2p_whoami() {
  20.  return get_the_author_meta( 'user_description', 1 );
  21. }

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
  1. include_once('/absolute/path/to/class-IXR.php');
  2.  
  3. Class P2PClient extends IXR_Client {
  4.  
  5. var $MyPeerConnection;
  6. var $response;
  7. var $url;
  8.  
  9. function __construct($url="") {
  10.                  $this->url = $url;
  11.     if(!$this->connect($url)) return false;
  12.      }
  13.      
  14. //see if there is an xml rpc endpoint
  15. function connect($url)
  16.      {
  17.        if(!$this->MyPeerConnection = new IXR_Client($this->url)) return false;
  18.      }
  19.  
  20.  //query 'p2p.whoami', store the response
  21. function whoareyou() {
  22.   $this->MyPeerConnection->query('p2p.whoami');
  23.   $this->response = $this->MyPeerConnection->getResponse();
  24.      }
  25. }
  26.  
  27. //init the client, query, echo response
  28. $p2p = new P2PClient('http://loaneys.com/xmlrpc.php');
  29. $p2p->whoareyou();
  30. 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 *