psx/http
Composer 安装命令:
composer require psx/http
包简介
Mutable HTTP model and HTTP client implementation
关键字:
README 文档
README
This library contains elegant interfaces to describe HTTP message, middleware and client classes. It contains also corresponding reference implementations which can be used by every app which needs a solid HTTP stack.
Interfaces
HTTP
HTTP Body
HTTP Middleware
HTTP Client
Examples
Middleware
The following shows a simple middleware which always returns the response body Hello World!:
<?php use PSX\Http; use PSX\Http\Server; $chain = new Http\Filter\FilterChain(); // enforce user agent in HTTP request $chain->on(new Http\Filter\UserAgentEnforcer()); // display maintenance file if available $chain->on(new Http\Filter\Backstage(__DIR__ . '/.maintenance.html')); // closure middleware $chain->on(function(Http\RequestInterface $request, Http\ResponseInterface $response, Http\FilterChainInterface $filterChain){ // get query parameter $request->getUri()->getParameter('foo'); // set header $response->setHeader('X-Foo', 'bar'); // write data to the body $response->getBody()->write('Hello World!'); $filterChain->handle($request, $response); }); // create global HTTP request and response $request = (new Server\RequestFactory())->createRequest(); $response = (new Server\ResponseFactory())->createResponse(); // start middleware chain $chain->handle($request, $response); // send response (new Server\Sender())->send($response);
Client
The following sends an HTTP GET request to google:
<?php use PSX\Http\Client; use PSX\Http\Exception\StatusCodeException; // create HTTP client $client = new Client\Client(); // build request $request = new Client\GetRequest('https://google.com', ['Accept' => 'text/html']); // send request $response = $client->request($request); // check response if ($response->getStatusCode() == 200) { // get header $contentType = $response->getHeader('Content-Type'); // output response body echo (string) $response->getBody(); } else { // the client never throws an exception for unsuccessful response codes but // you can do this explicit StatusCodeException::throwOnError($response); }
Uploads
Example how to handle file uploads:
<?php use PSX\Http; use PSX\Http\Server; $chain = new Http\Filter\FilterChain(); // closure middleware $chain->on(function(Http\RequestInterface $request, Http\ResponseInterface $response, Http\FilterChainInterface $filterChain){ // get body $body = $request->getBody(); if ($body instanceof Http\Stream\MultipartStream) { // move uploaded file to a new location $body->getPart('userfile')->move('/home/new/file.txt'); // or access the file directly through the normal stream functions //$body->getPart('userfile')->read(32); // write data to the body $response->getBody()->write('Upload successful!'); } else { // no upload so show form $html = <<<'HTML' <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /> </form> HTML; $response->getBody()->write($html); } $filterChain->handle($request, $response); }); // create global HTTP request and response $request = (new Server\RequestFactory())->createRequest(); $response = (new Server\ResponseFactory())->createResponse(); // start middleware chain $chain->handle($request, $response); // send response (new Server\Sender())->send($response);
psx/http 适用场景与选型建议
psx/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 247.45k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 03 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 psx/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 psx/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 psx/http 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
http客户端
Easily add Excepct-CT header to your project
Build mini web servers in PHP for testing
Cbox SSRF — a hardened, config-driven guard against server-side request forgery for outbound URLs in Laravel. Blocks private/reserved/cloud-metadata targets, pins DNS, and refuses redirects.
Eventually this library may have some useful HTTP-related helpers, but currently there's only HTTP response status codes.
统计信息
- 总下载量: 247.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 16
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-03-31