How to use Drupal into a custom PHP Script ?

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…

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

Or if you want your PHP script in your module folder

Now you can add Drupal headers after a chdir

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation