marcoazn89/http-wrapper

一个HTTP包装库

v2.1.1 2016-09-01 16:53 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:38:34 UTC


README

composer require marcoazn89/http-wrapper:dev-dev

特性

  • 遵守PSR-7的响应对象
  • 内容协商
  • 避免误打的常量
  • 在PSR-7之外使用的灵活性

创建新的响应对象

require '../vendor/autoload.php';

$response = new \HTTP\Response();

设置头部信息

require '../vendor/autoload.php';

(new \HTTP\Response())->withType(\HTTP\Response\ContentType::JSON)
->write(['greeting' => 'Hello World'])->send();

协商头部信息

require '../vendor/autoload.php';

//Assuming the client send Accept:text/plain
(new \HTTP\Response())->withTypeNegotiation()->write("Test")->send();

设置支持的极限

添加支持的顺序很重要!这将忽略任何不匹配支持类型的Accept头部信息。

require '../vendor/autoload.php';

use HTTP\Support\TypeSupport;
use HTTP\Response\ContentType;

// Add content you can support
TypeSupport::addSupport([
	ContentType::HTML,
	ContentType::XML
]);

// Assume the client sent XML as the accept header, the following output will be
// in XML form because it was the best match in the supported types
(new \HTTP\Response())->withTypeNegotiation()->write("<p>Hello World</p>")->send();