I had to make a Drupal data importation script in PHP to be processed every day. You have to follow a few steps to succeed.
First I had to remove PHP time out and memory limits to avoid failures. Drupal script can take a long time to execute, so many tables, queries, caches and joins every where…
1 2 3 4 |
set_time_limit(0); ini_set('memory_limit', -1); error_reporting(E_ALL); header('Content-Type: text/html; charset=utf-8'); |
Don’t forget to manually change your Apache settings for Linux or IIS Settings for Windows to increase time out connection value or you will have nice error results ;).
Before setting Drupal headers, you need to define DRUPAL_ROOT. It can be set to getcwd() if your PHP script will be in same folder as Drupal installation
1 |
define('DRUPAL_ROOT', getcwd()); |
Or if you want your PHP script in your module folder
1 |
define('DRUPAL_ROOT', str_replace("\sites\all\modules\custommodule","",getcwd())); |
Now you can add Drupal headers after a chdir
1 2 3 |
chdir(DRUPAL_ROOT); require_once './includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); |
drupal_bootstrap function can take multiple values according to what you’ll need into your PHP Script.
- DRUPAL_BOOTSTRAP_CONFIGURATION: Initializes configuration.
- DRUPAL_BOOTSTRAP_PAGE_CACHE: Tries to serve a cached page.
- DRUPAL_BOOTSTRAP_DATABASE: Initializes the database layer.
- DRUPAL_BOOTSTRAP_VARIABLES: Initializes the variable system.
- DRUPAL_BOOTSTRAP_SESSION: Initializes session handling.
- DRUPAL_BOOTSTRAP_PAGE_HEADER: Sets up the page header.
- DRUPAL_BOOTSTRAP_LANGUAGE: Finds out the language of the page.
- DRUPAL_BOOTSTRAP_FULL: Fully loads Drupal. Validates and fixes input data.
Recent Comments