Tag Archives: xml-rpc

Simple php xml-rpc client to wordpress

Quick example how to get categories:

<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://myblog.com/xmlrpc.php');

        $USER = 'admin';
        $PASS = 'mypassword';

        if (!$client->query('wp.getCategories','', $USER,$PASS))
        {  
            echo('Error occured during category request.' . $client->getErrorCode().":".$client->getErrorMessage());  
        }
        $cats = $client->getResponse();

        if(!empty($cats))
        {
            echo 'Categories:<br /><hr />';
            foreach($cats as $_cat) echo $_cat['categoryName']."<br />";
        }

?>

 adding a new post

<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://domain.com/xmlrpc.php');

        $USER = 'admin';
        $PASS = 'mypassword';

        $content['title'] = 'Test title '.mt_rand();
        $content['categories'] = array("NewCategory","Nothing");
        $content['description'] = '<p>Lorem ipsum dolor sit amet</p>';
        $content['custom_fields'] = array( array('key' => 'my_custom_fied','value'=>'yes') );
        $content['mt_keywords'] = array('foo','bar');

        if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, true))
        {
            die( 'Error while creating a new post' . $client->getErrorCode() ." : ". $client->getErrorMessage());  
        }
        $ID =  $client->getResponse();

        if($ID)
        {
            echo 'Post published with ID:#'.$ID;
        }

?>

XML-RPC Api specs: http://codex.wordpress.org/XML-RPC_WordPress_API

Full list of method calls as declared in the code: Continue reading