This is my boilerplate for stand alone php. I mainly use it for debugging so adjust accordingly.
if (isset($_SERVER['argc']) || isset($_SERVER['argv'])) {
// Set server variables if cli.
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_SOFTWARE'] = NULL;
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['QUERY_STRING'] = '';
$_SERVER['HTTP_USER_AGENT'] = 'console';
}
// Find index.php
$webroot = str_replace('\\', '/', dirname(__FILE__));
$webroot = str_replace('\\', '/', realpath(dirname($_SERVER['SCRIPT_FILENAME'])));
while (!empty($webroot)) {
if (file_exists($webroot . '/index.php') && strpos(file_get_contents($webroot . '/index.php'), 'menu_execute_active_handler();') !== FALSE) {
break;
}
$new_webroot = str_replace('\\', '/', dirname($webroot));
if ($new_webroot == $webroot) {
$webroot = str_replace('\\', '/', getcwd());
break;
}
$webroot = $new_webroot;
}
chdir($webroot);
/**
* Root directory of Drupal installation.
*/
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
// Set the base URL.
$base_url = 'http://' . $_SERVER['HTTP_HOST'];
// Set https to on if it should be.
if ( (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on")
|| (isset($_SERVER["HTTP_X_FORWARDED_PROTO"]) && $_SERVER["HTTP_X_FORWARDED_PROTO"] == "https")
|| (isset($_SERVER["HTTP_HTTPS"]) && $_SERVER["HTTP_HTTPS"] == "on")
) {
$_SERVER["HTTPS"] = "on";
}
// Fix base URL if HTTPS.
if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on" && isset($base_url)) {
$base_url = str_replace("http://", "https://", $base_url);
}
try {
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
ini_set('memory_limit', '8000M');
$output = array();
$time_start = microtime(true);
set_time_limit(0);
// header('Content-Type: text/plain');
// Run code here.
$time_end = microtime(true);
$time = round($time_end - $time_start, 4);
echo "took $time seconds<br>\n";
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}