Bootstrap and include Drupal from outside the Drupal root

Working on a site that needed to get some pretty detailed membership information as well as some payment processing the natural solution seemed like the Drupal based CiviCRM .  However, there was one problem and that was the CMS they had managing the rest of the site was actually in Worpdress, so i would need to bootstrap Drupal.

CiviCRM
Image via Wikipedia

Ideally if there was a CRM plugin with payment processing and some of the advanced features of CiviCRM then this would have been the natural choice. But seeing as none existed CiviCRM was the choice.

As a result we would also want to use some of the Drupal classes, methods, and functions so we needed to bootstrap Drupal outside of the Drupal root.

For example the site http://foo.com is the site root, and the Drupal root is http://foo.com/drupal. If the site needed to have a registration form for a page within their current path where the events are located, the ideal method would be to simply call the hook for the Drupal form from http://foo.com/events/party/event-1.

To do this you need to bootsrap the Drupal files like this

$drupal_directory = "/home/httpdocs/drupal";  // wherever Drupal is

$current_directory = getcwd();

chdir($drupal_directory);

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$sql = "SELECT node.title, node.type, node.nid FROM {node} WHERE node.type = '$node_type' ";
$output .= "";
$result = db_query($sql);
 while ($anode = db_fetch_object($result)) {
 $output .= l($anode->title, "node/$anode->nid")."<br />";
}

print $output;
// --------------------------------------
// ...
// ...
chdir($current_dir);
return;
 

There was one other way to do this and you can find how it was done here:

$_SERVER['SCRIPT_NAME'] = '/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/script.php';
$_SERVER['HTTP_HOST'] = 'mysite.example.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'POST';

chdir('/path/to/drupal/');
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

TheAnd there you have it, now you have full access to methods like: db_query(), db_fetch_object() and l() from outside of the Drupal root. Be sure to check out this post on the Drupal forum on how to bootstrap or include Drupal functions outside of the actual path.

  • Paul Arntz

    This was very helpful…thanks.

  • Paul Arntz

    This was very helpful…thanks.