Last updated 8 February 2009. Created on 24 August 2006.
Edited by Standart, jbrauer, pwolanin, sepeck. Log in to edit this page.

This is sample code to edit a Drupal page from another PHP site using cURL.

// ***********************************************
// ********* Login into drupal
// ***********************************************

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost/test/drupal/?q=node&destination=node");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP script');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=mylogin&pass=mypass&form_id=user_login_block");

ob_start();      // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean();  // stop preventing output
curl_close ($ch);
unset($ch);

// ***********************************************
// ********** get page create form (necesary for getting token)
// ***********************************************

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"http://localhost/test/drupal/?q=node/add/page");

$buf3 = curl_exec ($ch);

curl_close ($ch);

// find form token
preg_match('@edit-page-node-form-form-token" *value="([^"]*)"@',$buf3,$matches);
$token=$matches[1];
echo "token:$token\n";

// ***********************************************
// *********  post form, using token
// ***********************************************


$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,"http://localhost/test/drupal/?q=node/add/page");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "title=testtitle&body=testbody&status=1&revision=1&op=Submit&form_id=page_node_form&form_token=$token");

$buf2 = curl_exec ($ch);

curl_close ($ch);

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

amitaibu’s picture

1. I got page submitted as anonymous. I've changed all cookie.txt to d6_cookie.txt and it works properly.
2. Make sure php_cURL extension is installed in your PHP environment.

aaron1234nz’s picture

You also need to have the user login block enabled for the front page on the site your tying to login to. It doesn't work otherwise.

denis_sle’s picture

Your page was submitted as Anonymous, because of missing parameter name in node post query string:
instead of
curl_setopt($ch, CURLOPT_POSTFIELDS, "title=testtitle&body=testbody&status=1&revision=1&op=Submit&form_id=page_node_form&form_token=$token");
you have to add
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=mylogin&title=testtitle&body=testbody&status=1&revision=1&op=Submit&form_id=page_node_form&form_token=$token");

dalad’s picture

I don't know exactly how to explain this, but it happened to me that curl fails the login the first time it tries (when there is no cookie file on disk) and succeeds at the subsequent call. The solution to my problem was simply retrying, cycling two times through the

// ********* Login into drupal

part.

my empire of bytes: digitalillusion.altervista.org

ixodcom’s picture

Does this work or am I just not getting it? Login seems to work fine, but when it comes to form submit I get the following error....
"Validation error, please try again. If this error persists, please contact the site administrator."

I have several thousand nodes I would like to import, and if this works it would be a great solution for me.

I have a drupal 6.17 site, did this used to work?

-- Help?
--- Thx in advance.

ixodcom’s picture

I am using search engine friendly urls, so be sure to use the SEF url in the cURL script

USE this if using SEF urls
http://example.com/node/add/page

Not this
http://example.com/?q=node/add/page