Skip to content

Instantly share code, notes, and snippets.

@pyrou
Created May 27, 2015 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrou/166373a04b043fe3c145 to your computer and use it in GitHub Desktop.
Save pyrou/166373a04b043fe3c145 to your computer and use it in GitHub Desktop.
Run a php server if file launched from CLI (from PayPal-PHP-SDK)
<?php
if (PHP_SAPI == 'cli') {
// If the index.php is called using console, we would try to host
// the built in PHP Server
if (version_compare(phpversion(), '5.4.0', '>=') === true) {
//exec('php -S -t ' . __DIR__ . '/');
$cmd = "php -S localhost:5000 -t " . __DIR__;
$descriptors = array(
0 => array("pipe", "r"),
1 => STDOUT,
2 => STDERR,
);
$process = proc_open($cmd, $descriptors, $pipes);
if ($process === false) {
fprintf(STDERR,
"Unable to launch PHP's built-in web server.\n");
exit(2);
}
fclose($pipes[0]);
$exit = proc_close($process);
exit($exit);
} else {
echo "You must be running PHP version less than 5.4. You would have to manually host the website on your local web server.\n";
exit(2);
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment