phpixie/http
Composer 安装命令:
composer require phpixie/http
包简介
HTTP library for PHPixie, implements the PSR-7 standard
README 文档
README
PHPixie HTTP library
This library handles HTTP protocol asbtraction and implements the PSR-7 implemenetation. In addition to implementing the standard it provides wrappers for Requests and Responses and also abstractions for Cookies and Session. Since these wrappers work with any PSR-7 implementation it will now be possible to run PHPixie in some interesting environments, like inside ReactPHP etc. You can also use these abstractions to create your own PSR-7 compatible microframework.
Here is a quick demo:
//Without the PHPixie Framework $slice = new \PHPixie\Slice(); $http = new \PHPixie\HTTP($slice); //inside the framework $http = $frameworkBuilder->components()->http();
Requests
//Build a Request from globals ($_GET etc) $request = $http->request(); //Or you can pass a PSR-7 ServerRequestInterface to wrap $request = $http->request($serverRequest); //$_GET $query = $request->query(); //$_POST $query = $request->data(); //Additional attributes, //e.g. parameters from routing $query = $request->attributes(); //$_GET['pixie'] $query->get('pixie'); //With default value $query->get('pixie', 'Trixie'); //Throw an exception if field is not set $query->getRequired('pixie'); //$_GET['user']['name']; $query->get('user.name'); //Or like this $userData = $query->slice('user'); $userData->get('name'); //In this case $userData //is an instance of \PHPixie\Slice\Data //totally unrelated to HTTP library //so you can pass it around the system //without coupling to HTTP //Accessing $_SERVER $request->server()->get('http_host'); //Get a header line //If multiple values are present //for the same header, they will be //concatenated with ',' $request->headers()->get('host'); $request->headers()->getRequired('host'); //Get header values as array $request->headers()->getLines('accept'); //Handling uploads $uploadedFile = $request->uploads()->get('file'); $uploadedFile->move('/images/fairy.png'); //HTTP method $uri = $request->method(); //Accessing Uri $uri = $request->uri(); $path = $uri->getPath(); //Underlying ServerRequest $serverRequest = $request->serverRequest();
Response
Apart from provideing a Response wrapper, PHPixie HTTP also can simplify building some frequently used responses, like JSON responses with proper headers and file downloads.
$responses = $http->responses(); //The simplest response $response = $responses->string('hello world'); //JSON with headers that forbid caching $responses->json(array('name' => 'Pixie')); //Redirect $responses->redirect('http://phpixie.com/'); //File streaming $responses->streamFile('pixie.png'); //Download contetnts as a file //useful for CSV, TXT types $responses->download('name.txt', 'text/plain', 'Trixie'); //File download $responses->downloadFile('pixie.png', 'image.png', 'images/fairy.png'); //Modifying the status code $response->setStatus('404', 'Not Found'); //OR you can omit the phrase to use //the default one for the provided code $response->setStatus('404'); //Modifying headers $response->headers->set('Content-Type', 'text/csv'); //Transforming into PSR-7 Response $response->asResponseMessage(); //Outputting a response $http->output($response);
Context
Another important part is managing users Cookies and Session. Frequently you would access them outside of the request processing, for example in you authroization library. Also for non0standard environments, like in ReactPHP you would need special handlers for modifying the session. That’s why PHPixie separates these into a Context instance. You can store it separately, allowing users to modify cookies independently and then merge them with the Response. It’s rather easy:
//Generate context for a particular request $context = $http->context($request); //And then it's pretty straightforward $cookies = $context->cookies(); $session = $context->session(); $cookies->set('lang', 'en'); $session->getRequired('user_id'); //Remember to pass the context //when outputting the Response //or generation a PSR-7 response $http->output($response, $context); $response->asResponseMessage($context);
To obtain the HTTP Context when using the PHPixie framework, you need to get it from the framework context:
$httpContext = $frameworkBuilder->context()->httpContext(); $cookies = $httpContext->cookies(); $session = $httpContext->session();
Also you can just use the PSR-7 implementations without PHPixie wrappers:
//Get the PSR-7 factory $psr7 = $http->messages(); $serverRequest = $psr7->sapiServerRequest();
As all the other PHPixie libraries it is 100% unit tested and has a high codeclimate score (actually perfect score in this case)
phpixie/http 适用场景与选型建议
phpixie/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.04k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2015 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「psr-7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpixie/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpixie/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpixie/http 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Model of a web-based resource
Interfaces for web resource models and services to retrieve and create them
Interface for a factory to create Psr\Http\Message\StreamInterface objects
A PSR-7 compatible library for making CRUD API endpoints
Retrieve a WebResourceInterface instance over HTTP
统计信息
- 总下载量: 23.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 18
- 点击次数: 10
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-04-25