- b2evolution CMS Support Forums
- b2evolution Support
- Plugins & Extensions
- Unfinished - cURL Posting
1 adam Sep 26, 2008 16:14
Heyyyyyylo!
This is an example of the CURL code i'm using to post to my blogs remotely. I'm using this because I want to use a cron script to automatically post to my blog from RSS feeds.
It's currently unfinished due to URL Encoding issues (I THINK!) The problem is the html is being sent of the URL, which causes issues. So if you are going to use this, ensure you encode your HTML.
Anything simple will always work but more complicated things may be a bit harder to achieve.
//Your username and password
$username = "username"; // Username
$password = "password"; // Password
//We need a cookie pass to store our username and password details
$cookie_file_path = "/usr/local/www/b2evo/blogs/cron/cookies/ihatecheese"; // Cookie Path
//This is for file writing issues, if the cookie cannot be written, fail.
if(!file_put_contents($cookie_file_path, 'Cookiez')) {
die('No Cookies for you.');
}
//A few parameters and URL encoding.
$LOGINURL = "http://yoururl.com/htsrv/login.php";
$username = urlencode($username);
$password = urlencode($password);
$redirect_to = urlencode('/admin.php');
$login_action = urlencode('Log in!');
$login_q = '?login';
$pwd_salt = 'XwK8TYJxRojSmH6HDmyZYLCVVON7Jz017OlKBKknXKhGjouWUvqJYylf8Z7cr2th';
$POSTFIELDS = '?pwd_hash=&pwd_salt=' . $pwd_salt . '&login='. $username .'&pwd='. $password.'&redirect_to=' . $redirect_to . '&login_action[login]=' . $login_action . '&' . $login_q. '=admin';
//Starting out CURL on the login page.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Our post data
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Out cookies
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
curl_close($ch);
//Done
echo $result; //This will show the page if login worked.
//If it's wrong auth details, fail again.
if(strpos($result, 'Wrong login/password.') !== FALSE) {
die('Passed wrong username and password.' . "\n");
}
//Now to post the blog.
echo "posting blog";
//I Encode it myself, just to ensure the URL doesn't get escaped and the HTML doesn't get altered.
$content = "<div><p>This is simple data</p></div>";
//The content doesn't need URL encoding, but if it did the functions to do so are below.
//I do not use url_encode because I want the HTML to stay intact
$content = str_replace("=","%3D", $content);
$content = str_replace("?","%3F", $content);
$content = str_replace("&","%26", $content);
$content = str_replace("!","%21", $content);
$content = str_replace(",","%2C", $content);
$title = "The title of my blog";
$tags = "tags, that, I, will, use for, my, blog";
$blogdata='&post_title='.urlencode($title).'&content='.$content.'&item_tags='.urlencode($tags).'&blog=1&preview=1&more=1&preview_userid=1&post_category=1&action=create';
//I echo the data for debugging purposes.
echo $blogdata;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://yourdomain.com/admin.php?ctrl=items&filter=restore&blog=1');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $LOGINURL);
curl_setopt ($ch, CURLOPT_POSTFIELDS,$blogdata);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec($ch);
curl_close($ch);
//Display errors if any (Not working for some reason)
echo "No: ".curl_error($ch)."error:". curl_error($ch);
echo $result;
echo "finished";
Have fun.