
Who said you have to be logged on your WordPress dashboard to publish a new post on your blog? PHP and cURL can do it easily for you. Let see how.

Who said you have to be logged on your WordPress dashboard to publish a new post on your blog? PHP and cURL can do it easily for you. Let see how.
Here is the function. This code is not made for being used within WordPress, so don't paste it on your functions.php file (or any other).
Please note that you must activate the XMLRPC posting option in your WordPress blog. If this option isn't activated, the code will not be able to insert anything into your blog database. Another thing, make sure the XMLRPC functions are activated on your php.ini file.
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category)
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
?>
If you're interested in that kind of stuff (PHP/cURL) you should definitely have a look at the 10 awesome things to do with cURL list I published two days ago on my other blog Cats Who Code.
Credits : http://porn-sex-viagra-casino-spam.com/coding/poster-automatiquement-sur-wordpress-avec-php/
26 Responses
Made the work easier by that. Thanks a lot.
Very useful tip to post on wordpress outside of the actual wp instance. And curl is amazing for helping in such a job. Thanks for sharing.
Wow that’s a pretty nice tip from a site called Porn Sex Viagra Casino Spam. Ha!
@Chris Coyier : Glad to see you on here, I like your css-tricks.com blog
Yeah that site name is definitely funny…But the content is often very good.
Interesting domain name
) Great tip, but I doubt I’ll use it.
I really love your all blog posts, because every time when you publish a new post always that post containing good content and newPHP or other language code that always help me..
Thanks for sharing!
Great tip. Will try this tip.
Nice, the XMLRPC is easy in php compared to c# (wrapper with static url).
Interesting way of posting to a blog
Does anyone have any ideas on how to use this code to post values to custom fields? I’m using the following code, and everything works perfectly except for my custom fields: $link and $discount-type.
$title,
‘description’=>$body,
‘mt_allow_comments’=>1, // 1 to allow comments
‘mt_allow_pings’=>1, // 1 to allow trackbacks
‘post_type’=>’post’,
‘mt_keywords’=>$keywords,
‘categories’=>array($category),
‘discount-type’=>$discounttype,
‘link’=>$link
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request(‘metaWeblog.newPost’,$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
?>
as always copying stuff from other pages and do not include all info.
this time you don’t mentioned that $rpurl must point to your xmlrpc.php file on server, ex: http://www.myblog.com/xmlrpc.php
Bill – open xmlrpc.php and i think you’ll figureout it.
Bill, look there is function: set_custom_fields it requeres ‘custom_fields’ to be sent with your custom field and value.
why is $keywords assigned an empty value? Can I pass a keyword or an array of keywords to this function, or do i need to change something to pass an array like:
‘mt_keywords’=>array($keywords),
and, if I do that and only pass 1 keyword, does that get me into trouble?
Thanks man! You save my life! Now i could make my bot write a post as he get new data! (i think this is amazing!)
nice, thanks….
Hello, I’d like to use your script for posting on WordPress using a customized GUI, but when I try to use it, I get the following error:
“Call to undefined function xmlrpc_encode_request() in …/include/inc.utils.php on line 30″
I’ve pasted the code in a php file OUTSIDE the WordPress blog… I’ve activated XML-RPC from the WP Administration Panel… What’s wrong?
I forgot, I’m not able to access the php.ini file… Is there a manner to do the same thing without enabling XMLRPC functions on php.ini file? Maybe using an other external PHP class?
Thank you again,
Ale
Since I’m not able to edit php.ini file on my hosting service, I need to use cURL without the xmlrpc_encode_request() PHP function, and I’m trying to pass the XML request whithout using such function. I’m using the following code:
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords=”,$encoding=’UTF-8′) {
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$ch = curl_init();
$curlStr=”blogger.newPost
“.$username.”
“.$password.”
<title>”. $title .” </title><category> CATEGORY </category> BODY
1
“;
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlStr);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
return $results;
}
but it seems to be wrong: I get the following error:
Warning: curl_error(): supplied argument is not a valid cURL handle resource in …
Trackbacks: