3

I'm trying to call drupal_bootstrap from 'temp.php' in a subfolder called 'custom' my code as follow:

chdir('../');
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Once I call the temp.php I get Drupal homepage.

I also tried an explicit physical path but with no use, code as follow:

define('DRUPAL_ROOT', '/var/htdocs/drupal7');
require_once '../includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

temp.php located under the following folder structure:

- var
-- htdocs
--- drupal7
---- custom 
----- temp.php
| improve this question | | | | |
5

take a look at http://drupal.org/project/js they implemented a custom js.php that bootstraps drupal.

/**
 * @file
 * Callback page that serves custom JavaScript requests on a Drupal installation.
 */

/**
 * @var The Drupal root
 */
define('DRUPAL_ROOT', getcwd());

/**
 * @} End of "Required core files".
 */

/**
 * @name Required core files
 * @{
 * The minimal core files required to be able to run a js request
 */
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
require_once DRUPAL_ROOT . '/includes/common.inc';
require_once DRUPAL_ROOT . '/includes/module.inc';
require_once DRUPAL_ROOT . '/includes/unicode.inc';
require_once DRUPAL_ROOT . '/includes/file.inc';
/**
 * @} End of "Required core files".
 */

// Do basic bootstrap to make sure the database can be accessed
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
| improve this answer | | | | |
  • The above code will not work because my temp.php file in a subfolder and it will return warning because the the path is wrong. It work fine if my file under the root folder. – Aboodred1 Mar 15 '13 at 18:58
  • php.net/manual/de/function.chdir.php might help you out. but after all this is not about php programming. – Andre Baumeier Mar 15 '13 at 19:44
  • +1 Thanks for the help. Still my Drupal package is rendering the homepage with wrong css and javascript paths :( – Aboodred1 Mar 15 '13 at 20:16
  • you can always define the base path in your settings.php – Andre Baumeier May 2 '14 at 15:04
  • welcome back to Drupal SE. The code I posted in my question should be the correct way of doing it. My problem was with Devel module, so I disabled it and everything worked as expected. – Aboodred1 May 2 '14 at 16:57
5

I wrote a short post on this here: http://www.csdesignco.com/content/using-drupal-data-functions-and-session-variables-external-php-script

Basically, you only need to do what index.php does, but you can elevate the bootstrap to whatever level you need:

<?php
define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
$base_url = 'http://'.$_SERVER['HTTP_HOST'];
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
?>

So, for instance, if you just wanted to get something form the database (security aside, this is just an example):

<?php
define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']); // Define the DRUPAL_ROOT constant which is used in many places.
$base_url = 'http://'.$_SERVER['HTTP_HOST']; // Again, this is used in many places.
require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; // Include the boostrap file.
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION); // Start things up to whatever level you need.

print db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $_GET['nid']))->fetchColumn();
?>

you could hit example.com/my-file.php?nid=123 to see the node title for node 123.

I also wrote a tiny function which you can find in this issue (http://drupal.org/node/1922406) where I needed to get the current user's session but didn't want to elevate Drupal's bootstrap to DRUPAL_BOOTSTRAP_SESSION as the page load goes from ~ 20 milliseconds to over 1,000 milliseconds:

function _mymodule_js_session_user() {
  $session_name = session_name();
  if (!empty($session_name)) {
    if ($uid = db_query("SELECT uid FROM {sessions} WHERE sid = :sid", array(':sid' => $_COOKIE[$session_name]))->fetchColumn()) {
      $account = new stdClass();
      $account->uid = $uid;
      $account->roles = array();
      $account->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
      $account->roles += db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = :uid", array(':uid' => $account->uid))->fetchAllKeyed(0, 1);
      return $account;
    }
  }
  return FALSE;
}
| improve this answer | | | | |
  • Thanks for your response. I tried your idea, unfortunately it gave me the same result because my code located in a subfolder. It still return Drupal homepage. – Aboodred1 Mar 15 '13 at 18:55
  • Try define('DRUPAL_ROOT', '/var/htdocs/drupal7'); $base_url = 'http://'.$_SERVER['HTTP_HOST'].'/drupal7'; – Charlie Schliesser Mar 15 '13 at 19:31
  • +1 I tried the above code on fresh/new Drupal installation and it worked fine, Thanks. But still have problem with my current Drupal package. – Aboodred1 Mar 15 '13 at 20:05
  • This solution works for me also! Thx! – Michal Przybylowicz Jan 23 '15 at 0:25
  • Why do we need to define $base_url? – Елин Й. May 4 '15 at 11:20
0

Basically, this should be enough (or at least, it was enough for me to run Drupal (7.44 version) functions from test.php file located in the site root folder):

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.