I'm trying to build an auto-commenter that posts to Drupal sites. I'm pretty sure my code is good, but it looks like Drupal creates a form_build_id that's different every time you load the page. I have an ugly hack with file_get_contents that gets the value, but then when I try to post to the form with cURL the value must be different because it's not getting saved.
Can I load the page, get the value, and post to the form in the same session?
This is the code I'm using right now
The error checking stuff is really just there while figuring this out.
I've never built any tools like this before, but doing these by hand is a pain in the ass, so the option is to have one of my employees do it or automate it. I'd much rather automate. I know some of you must have this figured out already, any help is appreciated.
I'll be more than happy to add this to the war chest once I can get it figured out.
Can I load the page, get the value, and post to the form in the same session?
This is the code I'm using right now
Code:
// SETUP THE FORM DATA
$url = 'http://'.$cs.'/?q=comment/reply/'.$node;
$html = file_get_contents($url);
$form_build_id = strstr($html,'name="form_build_id" id="');
$form_build_id = strstr($form_build_id,'value="');
$form_build_id = substr($form_build_id,7);
$pos = strpos($form_build_id,'"');
$form_build_id = substr($form_build_id,0,$pos);
$post_data['form_build_id'] = $form_build_id;
$post_data['form_id'] = 'comment_form';
$post_data['name'] = $name;
$post_data['mail'] = $email;
$post_data['homepage'] = $website;
$post_data['comment'] = urlencode($comment);
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
// POST THE FORM
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . curl_error($ch);
curl_close($ch);
I've never built any tools like this before, but doing these by hand is a pain in the ass, so the option is to have one of my employees do it or automate it. I'd much rather automate. I know some of you must have this figured out already, any help is appreciated.
I'll be more than happy to add this to the war chest once I can get it figured out.