Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal: how to access to Drupal's APIs with a standalone php script?

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 ?

like image 227
aneuryzm Avatar asked Feb 16 '11 08:02

aneuryzm


3 Answers

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.

like image 187
Norio De Sousa Avatar answered Oct 21 '22 19:10

Norio De Sousa


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');
like image 5
toastyghost Avatar answered Oct 21 '22 18:10

toastyghost


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.

like image 2
John Slegers Avatar answered Oct 21 '22 19:10

John Slegers