Community Documentation

Edit a Drupal page from another PHP site using cURL

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

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

<?php
// ***********************************************
// ********* 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);
?>

Comments

1. I got page submitted as

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.

You also need to have the

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.

page submitted as anonymous

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");

I don't know exactly how to

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

Hmmm this one as me stumped...

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.

- Pat

I have it working now...

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

- Pat

About this page

Drupal version
Drupal 5.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.