Skip to content

Instantly share code, notes, and snippets.

@abemassry
Created July 25, 2012 15:54
Show Gist options
  • Save abemassry/3176917 to your computer and use it in GitHub Desktop.
Save abemassry/3176917 to your computer and use it in GitHub Desktop.
PHP script to log into a drupal site and update a form using curl, can be used with other sites besides drupal
<?php
//
// curl_post_update.php
// You can modify this script to make PHP use a webpage like a person sitting
// at a computer would use a webpage.
//
// tags: #php #curl #form
//
// This script is licensed under the JSON licence http://www.json.org/license.html
// "The Software shall be used for Good, not Evil." style license
//
// execute this script with /usr/bin/php curl_post_update.php
// or where your php binary is located
//
// Questions? github.com/abemassry
//
$curl = curl_init();
$url = "http://www.example.com";
// set $cookie to a file you have write access to, or a folder you have write access, can be /tmp/cookie.txt
$cookie = 'cookie.txt';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
$postdata=array(
"name" => "user_name",
"pass" => "user_pass",
"form_id" => "user_login",
"op" => "Log in",
);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $postdata);
// First Log in
$result=curl_exec($curl);
sleep(1);
$headers = curl_getinfo($curl);
if ($headers['url'] == $url) {
die("Cannot login.");
}
$url = "http://www.example.com/form";
curl_setopt($curl, CURLOPT_URL, $url);
// remove previous post fields
curl_setopt ($curl, CURLOPT_POSTFIELDS, '');
// get text of page into a variable
$result=curl_exec($curl);
sleep(1);
// you can use pure PHP for this but I found it quicker to use a shell command
$bid = exec("echo '$result' | grep form_build_id | awk '{print $4}' | awk -F '\"' '{print $2}'");
$token = exec("echo '$result' | grep form_token | awk '{print $4}' | awk -F '\"' '{print $2}'");
// grab fields from parsing the form then
// the form_build_id and form_token is unique for every form
// enter into post data with new form
// required data for this particular form was "title" and "body[und][0][value]"
$postdata = array(
"title" => "",
"changed" => "",
"form_build_id" => "$bid",
"form_token" => "$token",
"form_id" => "timeline_node_form",
"body[und][0][summary]" => "",
"body[und][0][value]" => "",
"body[und][0][format]" => "filtered_html",
"files[field_image_und_0]" => "",
"field_image[und][0][_weight]" => "0",
"field_image[und][0][fid]" => "0",
"field_image[und][0][display]" => "1",
"field_timeline_date[und][0][value][date]" => "",
"revision" => "1",
"log" => "",
"path[pathauto]" => "1",
"name" => "",
"date" => "",
"additional_settings__active_tab" => "edit-revision-information",
"status" => "1",
"additional_settings__active_tab" => "",
"op" => "Save"
);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $postdata);
$result=curl_exec($curl);
sleep(1);
$headers = curl_getinfo($curl);
if ($headers['url'] == $url) {
die("did not work\n\n\n\n$result\n\n\n\n$headers\n\n\n\n$bid\n$token\n");
}
// close curl connection
// delete the cookie
// sleep if you need to log in again and enter another form
curl_close ($curl);
unlink($cookie);
sleep(2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment