phpixie/http

PHPixie 的 HTTP 库,实现了 PSR-7 标准

3.2 2023-02-08 17:29 UTC

This package is auto-updated.

Last update: 2024-09-08 20:52:22 UTC


README

PHPixie HTTP 库

Build Status Test Coverage Code Climate HHVM Status

Author Source Code Software License Total Downloads

这个库处理 HTTP 协议抽象,并实现了 PSR-7 实现。除了实现标准之外,它还提供了 Requests 和 Responses 的包装器,以及 Cookies 和 Session 的抽象。由于这些包装器可以与任何 PSR-7 实现一起工作,现在可以在一些有趣的环境中运行 PHPixie,例如 ReactPHP 内部。您也可以使用这些抽象来创建自己的 PSR-7 兼容的微框架。

以下是一个快速演示

//Without the PHPixie Framework
$slice = new \PHPixie\Slice();
$http = new \PHPixie\HTTP($slice);

//inside the framework
$http = $frameworkBuilder->components()->http();

请求

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

响应
除了提供响应包装器外,PHPixie HTTP 还可以简化构建一些常用响应,例如带有正确头部的 JSON 响应和文件下载。

$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);

上下文
另一个重要部分是管理用户的 Cookies 和 Session。通常您会在请求处理之外访问它们,例如在您的授权库中。在 ReactPHP 等非标准环境中,您还需要特殊的处理器来修改会话。这就是为什么 PHPixie 将这些功能分离到一个 Context 实例中。您可以将其独立存储,使用户可以独立修改 Cookies,然后与 Response 合并。这相当简单

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

在使用 PHPixie 框架时获取 HTTP 上下文,您需要从框架上下文中获取它

$httpContext = $frameworkBuilder->context()->httpContext();
$cookies = $httpContext->cookies();
$session = $httpContext->session();

您还可以仅使用 PSR-7 实现,而不使用 PHPixie 包装器

//Get the PSR-7 factory
$psr7 = $http->messages();
$serverRequest = $psr7->sapiServerRequest();

与其他 PHPixie 库一样,它是 100% 单元测试的,并且具有高 codeclimate 分数(实际上在这个案例中是完美分数)