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

十年热恋 提交于 2019-11-26 21:18:43

问题


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 ?


回答1:


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.




回答2:


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');



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/5014244/drupal-how-to-access-to-drupals-apis-with-a-standalone-php-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!