How can I call a Drupal function or get the global variable in a PHP file which is located under the drupal installation folder. I doing it for the first time. Are there any files I need to include in my code in order to access the Drupal function or variables?
Asked
Active
Viewed 1.4k times
10
-
Possible duplicate: http://stackoverflow.com/questions/5014244/drupal-how-to-access-to-drupals-apis-with-a-standalone-php-script – Laxman13 Jun 29 '11 at 14:16
-
2I strongly recmmend that you consider writing a module instead of adding arbitrary PHP files for things that should be part of the website. And for scripts, you can write drush scripts (http://drupal.org/project/drush). – Berdir Jun 29 '11 at 16:21
-
@Berdir I am using it for testing purpose. – Dijo David Jun 30 '11 at 05:25
4 Answers
10
If the above explained example doesn't work try this:
$path = $_SERVER['DOCUMENT_ROOT'];
chdir($path."/drupal");
define('DRUPAL_ROOT', getcwd()); //the most important line
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Alexey
- 7,127
- 9
- 57
- 94
7
Taken from the linked question in the comment above
You need to Bootstrap Drupal in the external PHP file:
/** bootstrap Drupal **/
chdir("/path/to/drupal/site/htdocs");
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
Be sure to change the path to your Drupal installation, then add your code below the code posted above.

Laxman13
- 5,226
- 3
- 23
- 27
2
define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
The above code works for me, when the script is in my Drupal root directory. This loads absolutely everything, not just Drupal core, including contributed module hooks.

Andrew Jenkins
- 211
- 3
- 7
0
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
print_r($user);
-
This question already has an accepted answer. If your answer is an improvement, you should add some detail explaining why. – Tom Fenech Mar 21 '14 at 08:04