Publish a post on your WordPress blog using PHP + cURL
I think I should name my blog “Web Bot source codes” :P Lolz , today I will be posting something similar called WPBot – Wordpress bot. The PHP code which will publish a post on your post using cURL.This PHP is dependent on class file class.XMLHttpRequest.php ( You can get it here ).You can use this function for various things like publishing of scores , news , jokes etc on your blog . Use cron job method of automated things as used in Twitter Bot.
Source Code
<?php
include("class.XMLHttpRequest.php");
//URL of the wordpress blog
$link="http://localhost/chetanim/newwp/";
//Logins
$username="admin";
$password="admin";
//Title and body
$title=time();
$body="Hello ! World";
//Common setting
$setting=array();
$setting['User-Agent']="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.17) Gecko/2009122116 Firefox/3.0.17 GTB5";
$setting['Login-File']="wp-login.php";
$setting['Accept']="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$setting['Accept-Language']="en-us,en;q=0.5";
$setting['Accept-Charset']="ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$setting['Content-Type']="application/x-www-form-urlencoded; charset=UTF-8";
$setting['Reffer']="{$link}wp-login.php?redirect_to=http%3A%2F%2Flocalhost%2Fchetanim%2Fnewwp%2Fwp-admin%2F";
//Getting logged in to the blog and getting cookies in var
$req=new XMLHttpRequest();
$req->open("POST","{$link}{$setting['Login-File']}");
$req->setRequestHeader("Host","localhost");
$req->setRequestHeader("User-Agent",$setting['User-Agent']);
$req->setRequestHeader("Accept",$setting['Accept']);
$req->setRequestHeader("Accept-Language",$setting['Accept-Language']);
$req->setRequestHeader("Accept-Encoding","gzip,deflate");
$req->setRequestHeader("Accept-Charset",$setting['Accept-Charset']);
$req->setRequestHeader("Referer",$setting['Reffer']);
$req->send("log=".urlencode($username)."&pwd=".urlencode($password)."&wp-submit=Log+In&redirect_to=".urlencode($link)."wp-admin%2F&testcookie=1&");
$responseText=$req->responseText;
$cookie=$req->getResponseHeader('Set-Cookie');
$req->close();
//Cleaning and arranging cookie properly
$cookie=explode(";",$cookie);
$cookie_part1=explode(", ",$cookie[1]);
$cookie_part1=$cookie_part1[1];
$cookie_part2=str_replace("httponly, ","",$cookie[3]);
$cookie_part3=str_replace("httponly, ","",$cookie[5]);
$cookie=str_replace(" ",'',$cookie_part1.";".$cookie_part2.";".$cookie_part3.";");
//Posting the topic
$req=new XMLHttpRequest();
$req->open("POST","{$link}wp-admin/post.php");
$req->setRequestHeader("Host","localhost");
$req->setRequestHeader("User-Agent",$setting['User-Agent']);
$req->setRequestHeader("Accept",$setting['Accept']);
$req->setRequestHeader("Accept-Language",$setting['Accept-Language']);
$req->setRequestHeader("Accept-Encoding","gzip,deflate");
$req->setRequestHeader("Accept-Charset",$setting['Accept-Charset']);
$req->setRequestHeader("Content-Type",$setting['Content-Type']);
$req->setRequestHeader("X-Requested-With","XMLHttpRequest");
$req->setRequestHeader("Referer","{$link}wp-admin/");
$req->setRequestHeader("Cookie",$cookie);
$request="post_title=".urlencode($title)."&content=".rawurlencode($body)."&tags_input=&action=post-quickpress-publish&quickpress_post_ID=0&_wpnonce=647ecf8dea&_wp_http_referer=" .urlencode($link."/wp-admin")."&";
$req->send($request);
$responseText=$req->responseText;
preg_match('/<a href="(.*)">View post<\/a>/',$responseText,$postlink);
//The link of the post
$postlink=$postlink[1];
echo
$postlink;
?>
Do Report Bugs and comment your new ideas or creativity over here.
Regards,
Chetan Mendhe
SHashank Amin 11:48 am on February 6, 2010 Permalink |
Great … TFS
chetanweb 3:48 pm on February 6, 2010 Permalink |
Welcome !!