For testing purposes, or for executing Drupal-related operations but from a PHP script that runs outside of Drupal, I often create independent PHP files that are not meant to be Drupal modules but still require access to Drupal’s database, functions and modules.

Using the function drupal_bootstrap, located within includes/bootstrap.inc, your script can load Drupal and make all of Drupal’s functionality accessible to your script.

Common Usage

For access to the Drupal database layer only:
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

For access to all modules and functions:
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Here is an example PHP script that loads Drupal in order to gain access to a Drupal node:

//set the working directory to your Drupal root chdir('/home/public_html/drupal/'); //require the bootstrap include require_once './includes/bootstrap.inc'; //Load Drupal drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); //(loads everything, but doesn't render anything) //display a node print '
';
print_r(node_load(12));
print '
';

Comments

I'm using this code into an external script invoked using AJAX from a Drupal website.
All I need to do is get the current user uid:

global $user;
echo $user->uid;

But I'm always getting the guest uid instead of the current user id... Am I missing something?!
Thanks

Hi Jose,

Thanks for this snippet - that works perfectly for including Drupal from within the same server.

However, I now have the situation that I would like to create a Wordpress plugin which potentially sits on many different servers and executes a call to Drupal. Any idea on how to do that properly?

Thanks,
Jonas

Thanks for posting this. I want to run a query on the Drupal database...I took the SQL from a View to pull the fields. What would be the proper way to code this into my page once I've loded bootstrap (I assume all I nned is BOOTSTRAP_DATABASE). It doesn't seem to recognize the columns). Thanks for help.

this line of code chdir('/home/public_html/drupal/') help me to getting out of trouble.

Thanks.

I'm working on a CSV-to-node mass import script for a client and this code snippet was exactly what I needed. Thanks very much, JoseOnate.

I couldn't agree more....finally someone includes the ONE thing that you HAVE to have to get this stuff to work!! Thanks a million!!!

The tip about changing directory really helped me. From a white screen of death to a page displaying the information I needed from Drupal. Thank you so much.

Anyone tried to embed the login form from Drupal in an external website on the same server? If succesful: what did you do to make it work?