command line
Drupal Command Line Script Template
Submitted by dale on March 23, 2009 - 10:09amNote: Drush is now a better option for running Drupal scripts. Please see this article for more information: Drupal Command Line Scripts with Drush.
There are development tasks better suited to running in a command-line script than in the Drupal web interface. The two most notable in my experience are code fragment testing, especially when exploring how an API works, and data import/export. Happily, this kind of scripting is possible with Drupal.
The PHP code shown below, created from examples seen in Planet Drupal posts, and later in the drush module, is serving me well as a command-line script template. It works in both Drupal 5 and 6.
Script Template
<?php
$stdout = fopen('php://stdout', 'w');
fwrite($stdout, "Script Template\n");
// Site specific variables
$username = "Dale";
$drupal_base_url = parse_url('http://www.example.com');
$_SERVER['HTTP_HOST'] = $drupal_base_url['host'];
$_SERVER['PHP_SELF'] = $drupal_base_url['path'].'/index.php';
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
$_SERVER['REMOTE_ADDR'] = NULL;
$_SERVER['REQUEST_METHOD'] = NULL;
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
$user = user_load(array('name' => $username));
//
//
// Drupal code here
//
//
?>Usage Notes
