weew/http-server

Simple wrapper for the php's built in http server.

v1.1.1 2016-07-21 11:18 UTC

This package is not auto-updated.

Last update: 2024-11-05 23:12:17 UTC


README

Build Status Code Quality Test Coverage Dependencies Version Licence

Table of contents

Installation

composer require weew/http-server

Basic usage

To start the server, simply pass in a hostname, desired port and the root directory for your server.

// all files within the given directory will be available
// at http://localhost:9999, if you've passed a file
// name instead of a directory the server will always serve this
// file, no matter how the URI looks like
$server = new HttpServer('localhost', 9999, __DIR__);

// incoming requests will be logged to this file
$server->setLogFile('/tmp/log');

$server->start();
$server->isRunning(); // true
$server->stop();

Advanced options

You can tell the server to block current process until the server has started by passing in a $waitForProcess value. You can also disable the server output completely.

// starting the server will wait for the server to start
// for a maximum of 2 seconds, then it will throw an exception
// saying that the process took too long to start
// the default value is 5.0 seconds
$waitForProcess = 2.0;
// enables log messages like
// [HTTP SERVER] Wed, 12 Aug 2015 19:49:25 +0200 - HTTP server started on localhost:9999 with PID 99412
// [HTTP SERVER] Wed, 12 Aug 2015 19:56:18 +0200 - Server is already running at localhost:9999 with PID 99535
// [HTTP SERVER] Wed, 12 Aug 2015 19:49:25 +0200 - Killing process with PID 99412
$enableOutput = true;

$server = new HttpServer('localhost', 9999, __DIR__, $waitForProcess, $enableOutput);
$server->start();

Related Projects

  • HTTP Blueprint: spin up a server, serve some content, shutdown the server.