Drupal: Simple way to bootstrap scripts into Drupal system

Previously I wrote about a method of hooking up an existing site with the Drupal backend. That will work fine if the request comes from the browser, but for a script you will need an extra little bit of configuration.

In conjuction with initialise_drupal_bootstrap() (a function provided in the previous post), you will need to call the following.

function initialise_for_script($server_address = 'yoursite.com') {
global $_SERVER;

// define default settings
$_SERVER['HTTP_HOST'] = $server_address;
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_SOFTWARE'] = 'PHP CLI';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['QUERY_STRING'] = '';
$_SERVER['PHP_SELF'] = $_SERVER['REQUEST_URI'] = '/';
}

When using the bootstrappers, your script would look something like this:

/*
* Run this from your Drupal folder.
* Usage: php scripts/mailout.php
*/

require_once('./scripts/drupal_bootstrap.inc');
initialise_for_script('examplesite.com');
initialise_drupal_bootstrap();

if (function_exists(mailout_loop')) {
mailout_loop();
}
 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog