Calling Drupal functions in external PHP file

2019-02-08 23:11发布

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?

4条回答
Bombasti
2楼-- · 2019-02-08 23:45

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);
查看更多
男人必须洒脱
3楼-- · 2019-02-08 23:54
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.

查看更多
看我几分像从前
4楼-- · 2019-02-08 23:56

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.

查看更多
Rolldiameter
5楼-- · 2019-02-08 23:59
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
print_r($user);
查看更多
登录 后发表回答