Skip to content

Instantly share code, notes, and snippets.

@a-r-m-i-n
Last active January 12, 2018 19:28
Show Gist options
  • Save a-r-m-i-n/bfc203c9146e42c59d6832693990cee3 to your computer and use it in GitHub Desktop.
Save a-r-m-i-n/bfc203c9146e42c59d6832693990cee3 to your computer and use it in GitHub Desktop.
Run local webserver in PHP and open default browser (on windows)
{
"require": {
"symfony/process": "^3.2"
}
}
<?php
use Symfony\Component\Process\Process;
require_once('vendor/autoload.php');
$host = 'localhost';
$port = '8080';
$htdocs = './app';
$timeout = 5; // seconds
$url = $host . ':' . $port;
$cmd = PHP_BINARY . ' -S ' . $url . ' -t ' . $htdocs;
$webserverProcess = new Process($cmd, __DIR__, null, null, null);
$webserverProcess->start();
if ('\\' === DIRECTORY_SEPARATOR && $webserverProcess->isRunning()) {
$errno = 0;
$errstr = '';
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
if ($fp) {
fclose($fp);
$openWebsiteCommand = DIRECTORY_SEPARATOR === '\\' ? 'explorer ' : 'open ';
$startBrowser = new Process($openWebsiteCommand . escapeshellarg('http://' . $url));
$startBrowser->run();
echo "To exit webserver press ctrl+c";
} else {
echo "Error when starting webserver!" . PHP_EOL;
}
}
$webserverProcess->wait();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment