phppkg/http-message

http message library of the php, implement the PSR7

v2.0.0 2021-12-08 11:54 UTC

This package is auto-updated.

Last update: 2024-11-06 08:29:12 UTC


README

License Php Version Latest Stable Version

http message 库,实现自 PSR 7。

安装

  • 通过 composer.json

编辑 composer.json,在 require 添加

"phppkg/http-message": "dev-master",

保存,然后执行: composer update

  • 通过 composer require
composer require phppkg/http-message
  • git拉取
git clone https://github.com/phppkg/http-message.git // github

使用

基本使用

use PhpPkg\Http\Message\Request;
use PhpPkg\Http\Message\Response;

$request = new Request($method, $uri);
$request = new ServerRequest(... ...);
$response = new Response($code);
... ...

工厂方法

使用提供的工厂方法可以快速创建想要的实例对象。

use PhpPkg\Http\Message\HttpFactory;

$request = HttpFactory::createRequest($method, $uri);

// server request
$request = HttpFactory::createServerRequest('GET', 'http://www.abc.com/home');
$request = HttpFactory::createServerRequestFromArray($_SERVER);

$response = HttpFactory::createResponse($code);

扩展

use PhpPkg\Http\Message\Request;
use PhpPkg\Http\Message\Traits\ExtendedRequestTrait;

class MyRequest extends Request {
   use ExtendedRequestTrait; // 里面提供的更多方便使用的方法
}

// 

$request = new MyRequest(...);

$age = $request->getInt('age');
$name = $request->getTrimmed('name');
use PhpPkg\Http\Message\Response;
use PhpPkg\Http\Message\Traits\ExtendedResponseTrait;

class MyResponse extends Response {
   use ExtendedResponseTrait;
}

项目地址

License

MIT