Submitted by newaction on Fri, 2012-01-13 12:56
The Drupal API is very useful, but it cannot be used easily via the command line.
This code allows a PHP shell script to masquerade as a normal request so the Drupal API loads and can be used correctly.
-----------------------------------------------------------------------------------
#!/usr/bin/php -q <?php // set some server variables so Drupal doesn't freak out $_SERVER['SCRIPT_NAME'] = '/script.php'; $_SERVER['SCRIPT_FILENAME'] = '/script.php'; $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['REQUEST_METHOD'] = 'POST'; // act as the first user global $user; $user->uid = 1; // change to the Drupal directory chdir('/path/to/drupal'); // Drupal bootstrap throws some errors when run via command line // so we tone down error reporting temporarily error_reporting(E_ERROR | E_PARSE); // run the initial Drupal bootstrap process require_once('includes/bootstrap.inc'); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // restore error reporting to its normal setting error_reporting(E_ALL);
Ref: http://wiki.ceejayoz.com/w/Using_the_Drupal_API_from_the_command_line
- Login to post comments