January 2009
M T W T F S S
    Feb »
 1234
567891011
12131415161718
19202122232425
262728293031  

WordPress, XMLRPC, MetaWeblog API and xmlrpc-2.2.1 library for PHP

Just an example of using xmlrpc-2.2.1 library for PHP, for creating a new post on a WordPress based blog using MetaWeblog.newPost xml/rpc function.

Of course, you’ll need xmlrpc-2.2.1 library that may be found here. MetaWeblog API documentation may be found here.

// Testing metaWeblogApi
require_once dirname(__FILE__)."/xmlrpc-2.2.1/lib/xmlrpc.inc"; // This should be changed with the place you've the xmlrpc.inc file if xmlrpc-2.2.1 library
 
define ('DOMAIN', 'domain.com'); // Put your domain instead
define ('BLOGID', 0); // Put your blog id instead
define ('USER', 'username'); // Put your username instead
define ('PASSWORD', 'userpassword'); // Put your password instead
 
 
// Create client
$cl = new xmlrpc_client ( "/xmlrpc.php", DOMAIN, 80);
 
// Prepare request
$req = new xmlrpcmsg('metaWeblog.newPost');
// Parameters one by one to be more clear:
$req->addParam ( new xmlrpcval ( BLOGID, 'int')); // blog_id
$req->addParam ( new xmlrpcval ( USER, 'string' )); // username
$req->addParam ( new xmlrpcval ( PASSWORD, 'string' )); // password
$struct = new xmlrpcval (
	array (
		"title" => new xmlrpcval ( 'metaWeblog.newPost', 'string' ), // title
		"link" => new xmlrpcval ( 'http://'.DOMAIN.'/metaweblog_test.html', 'string'), // link
		"description" => new xmlrpcval ( 'Test using metaWeblog.newPost for creating a new post on our blog.', 'string'), // description
	), "struct"
);
$req->addParam ( $struct );
$req->addParam ( new xmlrpcval (1, 'int')); // publish
 
// Send request
$ans = $cl->send($req);
 
var_dump ( $ans );

4 comments to WordPress, XMLRPC, MetaWeblog API and xmlrpc-2.2.1 library for PHP

  • Puneet

    when I run this code on my local server and wordpress installation I got this to display( and no post was added)
    object(xmlrpcresp)#10 (9) {
    ["val"]=>
    int(0)
    ["valtyp"]=>
    NULL
    ["errno"]=>
    int(5)
    ["errstr"]=>
    string(88) “Connect error: php_network_getaddresses: getaddrinfo failed: No such host is known. (0)”
    ["payload"]=>
    NULL
    ["hdrs"]=>
    array(0) {
    }
    ["_cookies"]=>
    array(0) {
    }
    ["content_type"]=>
    string(8) “text/xml”
    ["raw_data"]=>
    string(0) “”
    }

    Can u help me to know what is wrong?

  • Did you change DOMAIN by the name of your host?

  • Dam

    Hey thanks for the script, it works for me after testing 15 others lol

  • Dam

    May you please add more function like stated in XMLRPC.php file in function mw_newPost($args)

    // We’ve got all the data — post it:
    $postdata = compact(’post_author’, ‘post_date’, ‘post_date_gmt’, ‘post_content’, ‘post_title’, ‘post_category’, ‘post_status’, ‘post_excerpt’, ‘comment_status’, ‘ping_status’, ‘to_ping’, ‘post_type’, ‘post_name’, ‘post_password’, ‘post_parent’, ‘menu_order’, ‘tags_input’, ‘page_template’);

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

CAPTCHA image