9

When I create a new script in a separate php file to run for Drupal, I need to add the following lines on top in order to access all Drupal APIs:

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

Is this correct ?

aneuryzm
  • 63,052
  • 100
  • 273
  • 488

4 Answers4

13

Yep, I use this:

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

And then just add whatever Drupal-specific code you need below that.

  • Also, check the documentation of drupal_bootstrap to see which bootstrap level you actually need. – Pierre Buyle Feb 17 '11 at 09:16
  • The question was about accessing "all Drupal APIs" - is it really THAT simple, or do dependencies rear their ugly head? Example: using Drupal Forms API in an external PHP script (because FAPI is awesome). Not for lack of trying, but I haven't found any blog posts demonstrating Drupal API usage outside of Drupal. If anyone has such links, it's on-topic and much appreciated! :-) – Scott Prive Mar 27 '12 at 14:19
5

this method still works with drupal 7, but instead of the chdir bit you may need to add the following line before the require and bootstrap call:

define('DRUPAL_ROOT','/path/to/drupal');
toastyghost
  • 51
  • 1
  • 4
2

This should work for both Drupal 6 and Drupal 7 :

define('DRUPAL_ROOT', 'path/to/drupal');
chdir(DRUPAL_ROOT);
require './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

It doesn't matter where you put the script that contains this code. Just make sure you replace path/to/drupal with the actual installation path of your Drupal system.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
0

yes, it's one way to enter drupal api.
Sometime chdir("dir to drupal dir"); required, if you call php script from other directory.

Nikit
  • 5,128
  • 19
  • 31