Skip to content

php http message library, implement the psr7 http-message interface

License

Notifications You must be signed in to change notification settings

phppkg/http-message

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

http message

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