Post Pic

Post on your WordPress blog using PHP

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/

Related Posts

Related Posts

No related posts.

26 Responses

Jul 01 2009 11:27

Made the work easier by that. Thanks a lot.

Jul 01 2009 16:38

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.

Jul 01 2009 19:38

Wow that’s a pretty nice tip from a site called Porn Sex Viagra Casino Spam. Ha!

Jul 02 2009 00:14

@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.

Jul 02 2009 17:33

Interesting domain name :) ) Great tip, but I doubt I’ll use it.

Jul 02 2009 18:58

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..

Jul 04 2009 12:07

Thanks for sharing!

Jul 07 2009 12:17

Great tip. Will try this tip.

Jul 13 2009 00:15

Nice, the XMLRPC is easy in php compared to c# (wrapper with static url).

Jul 14 2009 11:50

Interesting way of posting to a blog

Jul 17 2009 03:40

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;
}
?>

Aug 08 2009 00:53

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

Aug 08 2009 00:59

Bill – open xmlrpc.php and i think you’ll figureout it. :)

Aug 08 2009 01:06

Bill, look there is function: set_custom_fields it requeres ‘custom_fields’ to be sent with your custom field and value.

Aug 17 2009 06:47

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?

Aug 18 2009 03:19

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!)

Nov 25 2009 20:46

nice, thanks….

Jan 08 2010 16:20

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?

Jan 08 2010 16:28

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

Jan 08 2010 18:40

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:

Leave a Comment

* Name, Email, Comment are Required