Skip to content

Instantly share code, notes, and snippets.

@abemassry
Created July 27, 2012 18:30
Show Gist options
  • Save abemassry/3189632 to your computer and use it in GitHub Desktop.
Save abemassry/3189632 to your computer and use it in GitHub Desktop.
PHP script to log into a drupal site and update a form and upload using curl
<?php
//
// this gist takes most of the code from https://gist.github.com/3176917 but adds in form uploading which is just an @ symbol
// this also explores different options for the form data
//
// curl_post_upload.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 #upload
//
// 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(
"files[file_und_0]" => "@upload_doc.pdf",
"field_doc_number[und][0][value]" => "399",
"title" => "The Title",
"changed" => "",
"form_build_id" => "$bid",
"form_token" => "$token",
"form_id" => "node_form",
"field_date[und][0][value][date]" => "12/02/2001",
"revision" => "1",
"path[pathauto]" => "1",
"name" => "",
"date" => "",
"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