weew/http-server

PHP内置http服务器的简单封装。

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

This package is not auto-updated.

Last update: 2024-09-10 22:18:32 UTC


README

Build Status Code Quality Test Coverage Dependencies Version Licence

目录

安装

composer require weew/http-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();

高级选项

你可以通过传入$waitForProcess值来告诉服务器在启动服务器之前阻塞当前进程。你也可以完全禁用服务器输出。

// 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();

相关项目

  • HTTP Blueprint: 启动服务器,提供一些内容,关闭服务器。