定制 wp-php-toolkit/http-server 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wp-php-toolkit/http-server

Composer 安装命令:

composer require wp-php-toolkit/http-server

包简介

HttpServer component for WordPress.

README 文档

README

slug httpserver
title HttpServer
install wp-php-toolkit/http-server
see_also
cli | CLI | Expose a local browser UI from a command-line tool. httpclient | HttpClient | Test client code against a small local fixture server.

A minimal blocking TCP HTTP server in pure PHP. For CLI tools and tests, not for production traffic.

Why this exists

Sometimes a PHP tool needs a tiny local HTTP surface: a test fixture server, a webhook receiver during development, a CLI tool with a browser UI, or a demo endpoint for another component. Pulling in a production web framework would obscure the example and add dependencies the toolkit avoids.

The HttpServer component is intentionally small: a blocking TCP server, incoming request objects, and response writers. It is useful for local tools and tests. It is not a replacement for nginx, Apache, php-fpm, RoadRunner, Swoole, or a production application server.

Hello world on port 8080

Run on your machine: the Playground sandbox does not allow processes to bind listening TCP ports. Save this snippet locally and run php hello-server.php.

<?php
require __DIR__ . '/vendor/autoload.php';

use WordPress\HttpServer\TcpServer;
use WordPress\HttpServer\IncomingRequest;
use WordPress\HttpServer\Response\ResponseWriteStream;

$server = new TcpServer( '127.0.0.1', 8080 );

$server->set_handler( function ( IncomingRequest $request, ResponseWriteStream $response ) {
	$response->send_http_code( 200 );
	$response->send_header( 'Content-Type', 'text/plain' );
	$response->append_bytes( "Hello from " . $request->method . " " . $request->url . "\n" );
} );

$server->serve( function ( $host, $port ) {
	echo "Listening on http://{$host}:{$port}\n";
} );

A tiny JSON router

Run on your machine: needs a listening port. Once running, try curl localhost:8080/api/status.

Build a CLI tool with a web UI by switching on the parsed path and method.

<?php
require __DIR__ . '/vendor/autoload.php';

use WordPress\HttpServer\TcpServer;
use WordPress\HttpServer\IncomingRequest;
use WordPress\HttpServer\Response\ResponseWriteStream;

$server = new TcpServer( '127.0.0.1', 8080 );

$server->set_handler( function ( IncomingRequest $request, ResponseWriteStream $response ) {
	$path = $request->get_parsed_url()->pathname;

	if ( '/api/status' === $path ) {
		$response->send_http_code( 200 );
		$response->send_header( 'Content-Type', 'application/json' );
		$response->append_bytes( json_encode( array(
			'ok'     => true,
			'pid'    => getmypid(),
			'memory' => memory_get_usage( true ),
		) ) );
		return;
	}

	if ( '/api/echo' === $path && 'POST' === $request->method ) {
		$body = '';
		while ( ! $request->body_stream->reached_end_of_data() ) {
			$n = $request->body_stream->pull( 4096 );
			if ( $n > 0 ) $body .= $request->body_stream->consume( $n );
		}
		$response->send_http_code( 200 );
		$response->send_header( 'Content-Type', 'text/plain' );
		$response->append_bytes( $body );
		return;
	}

	$response->send_http_code( 404 );
	$response->append_bytes( "Not found\n" );
} );

$server->serve();

Buffered response with auto Content-Length

Use BufferingResponseWriter when you want the framework to compute Content-Length for you, or when the runtime is CGI-shaped and expects the full body up front. This one runs anywhere — no socket required.

<?php
require '/php-toolkit/vendor/autoload.php';

use WordPress\HttpServer\Response\BufferingResponseWriter;

$writer = new BufferingResponseWriter();
$writer->send_http_code( 200 );
$writer->send_header( 'Content-Type', 'text/html' );
$writer->append_bytes( '<!doctype html><title>Hi</title><h1>Hello</h1>' );
$writer->append_bytes( '<p>Buffered body, sent at the end.</p>' );

ob_start();
$writer->close_writing();
$response_body = ob_get_clean();

echo "headers before send:\n";
foreach ( $writer->get_buffered_headers() as $name => $value ) {
	echo "{$name}: {$value}\n";
}
echo "\nbody:\n" . $response_body;
headers before send:
Content-Type: text/html

body:
<!doctype html><title>Hi</title><h1>Hello</h1><p>Buffered body, sent at the end.</p>

wp-php-toolkit/http-server 适用场景与选型建议

wp-php-toolkit/http-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 101 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「php-toolkit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 wp-php-toolkit/http-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wp-php-toolkit/http-server 我们能提供哪些服务?
定制开发 / 二次开发

基于 wp-php-toolkit/http-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 101
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2025-09-06