Posting comments with PHP and curl automatically

Here is an experiment of posting comments with php curl library, this is the part of glurt seo tools. With a little imagination you may create a great auto commenting software for any blog cms and any website to promote your website on web or for another purposes. I created this script because I was lazy to enter the name, email address again and again. I’ve come with the curl and PHP solution to post comments on wordpress blogs that I am interested in by typing url only inside of form field on my localhost, clicking submit , and the comment is there, sent by curl with PHP.
- POST DATA we will need
First, before creating the script I figured out which POST variables are being transferred by wordpress comment form, form action (where the comment submission process done). I opened the wordpress post’s html source (in mozilla firefox right mouse click->view source code) I found something similar to :
<form action=”http://blog.php/wp-comments-post.php” method=”post” id=”commentform”>
<p>
<input type=”text” name=”author” id=”author” value=”" size=”22″ tabindex=”1″ />
<label for=”author”><small>Name
(required) </small></label>
</p>
<p>
<input type=”text” name=”email” id=”email” value=”" size=”22″ tabindex=”2″ />
<label for=”email”><small>Mail (will not be published)
(required) </small></label>
</p>
<p>
<input type=”text” name=”url” id=”url” value=”" size=”22″ tabindex=”3″ />
<label for=”url”><small>Website</small></label>
</p>
<!–<p><small><strong>XHTML:</strong> You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> </small></p>–>
<p>
<textarea name=”comment” id=”comment” cols=”100%” rows=”10″ tabindex=”4″></textarea>
</p>
<p>
<input name=”submit” type=”submit” id=”submit” tabindex=”5″ value=”Submit Comment” />
<input type=”hidden” name=”comment_post_ID” value=”287″ />
</p>
</form>
The variables we will use for cron execution from form above are :
Form action url: http://blog.php/wp-comments-post.php – wordpress uses “wp-comments-post.php” file as a file for comment storing into the database.
Author : is the name of comment author, most of the wordpress blogs require it .
Email : Is a commenter email
Url : Is a url of your website
Submit : this variable is necessary for submitting the result in “wp-comments-post.php”.
comment_post_ID: is a hidden value, necessary for wp database that tells the script which post to bind comment to.
Author and email are required fields for cron operation, post id is also necessary because we should know where to post the comment. comment_post_ID is also neccessary to tell the script in which post comment will appear.
2. Getting wordpress post id comment_post_ID from wordpress post
To get post id to use it for curl form submission we will need comment_post_ID from post form . We will crawl the html with curl and take comment_post_ID variable in numeric format using regular expressions:
<?php
// CURL function to get the page and to insert it inside php variable.
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
function executeComment($name,$email,$url,$comment,$baseUrl,$postId)
{
$curlPost = ‘author=’.$name.’&email=’.$email.’&url=’.$url.’&comment=’ . urlencode($comment) . ‘&submit=Submit+Comment&comment_post_ID=’.$postId;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘http://’.$baseUrl.’/wp-comments-post.php’);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);
curl_close($ch);
}
function getDomain($url)
{
$pt = explode(”/”,$url);
$burl = $pt[2];
$result = get_web_page( $url );
return $result;
}
// this is an url to post comment on …
$url = ‘http://www.branded07.com/2009/07/11/ie6-web-design-tricks/’;
// we need www.branded07.com domain name for future usage , we will use our getDomain() function
$domain = getDomain($url);
// now we need to crawl the page and to get post ID
// we crawling the post url
$result = get_web_page( $url );
// page content is in the $page variable
$page = $result['content'];
// getting the post ID with regular expressions
$matches = array();
preg_match( ‘!<input.*?name=”comment_post_ID”.*?value=”(.*?)”!’, $page, $matches );
// this is a comment id to post comment
$pid = $matches[1];
// execute comment posting with curl
$name = “glurt”;
$email = “admin@glurt.com”;
$url = “http://glurt.com”;
$comment = “This is the test comment”;
$baseUrl = $domain;
$postId = $pid;
executeComment($name,$email,$url,$comment,$baseUrl,$postId);
?>
Conclusion :
We figured out what variables uses wordpress blog to post comments, we used this data to create comments posting script that needs only one url of wordpress web blog.
