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;
}