【发布时间】:2011-07-08 19:32:51
【问题描述】:
我想让一个非 Drupal 网站在提交表单时在我的 Drupal 安装中创建一个用户帐户。有没有办法在不在 drupal 模块内的 php 脚本中包含(或以其他方式启动)drupal API?我曾尝试包含一些包含文件,因为“未知功能”错误不断出现,但它已经变成了猫捉老鼠的游戏。
感谢您的帮助, 杰夫
【问题讨论】:
我想让一个非 Drupal 网站在提交表单时在我的 Drupal 安装中创建一个用户帐户。有没有办法在不在 drupal 模块内的 php 脚本中包含(或以其他方式启动)drupal API?我曾尝试包含一些包含文件,因为“未知功能”错误不断出现,但它已经变成了猫捉老鼠的游戏。
感谢您的帮助, 杰夫
【问题讨论】:
是的,使用services.module。它为 Drupal 提供 API。
或者:
<?php
chdir('/path/to/drupal/');
require_once('includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
编辑:根据 cmets,对于命令行使用,您必须欺骗 Drupal 依赖的 $_SERVER 东西(尤其是域名):
<?php
// set some server variables so Drupal doesn't freak out
$_SERVER['SCRIPT_NAME'] = '/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/script.php';
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
// act as the first user
global $user;
$user->uid = 1;
// change to the Drupal directory
chdir('/path/to/drupal');
// Drupal bootstrap throws some errors when run via command line
// so we tone down error reporting temporarily
error_reporting(E_ERROR | E_PARSE);
// run the initial Drupal bootstrap process
require_once('includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// restore error reporting to its normal setting
error_reporting(E_ALL);
【讨论】:
$_SERVER 东西。 wiki.ceejayoz.com/w/Using_the_Drupal_API_from_the_command_line