bulldog / http-factory
符合PSR-17规范的HTTP工厂
v1.0.1
2020-10-22 12:20 UTC
Requires
- guzzlehttp/psr7: ^1.4
- psr/http-factory: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.2
- zendframework/zend-diactoros: ^1.8
Provides
This package is auto-updated.
Last update: 2024-08-22 21:35:50 UTC
README
这个库旨在提供一个简单的PSR-17(HTTP Factories)实现。目前它支持Guzzle/Psr7和Zend Diactoros。以下为安装和使用示例,但如果有任何问题或疑虑,请创建一个问题。
安装
composer require bulldog/http-factory
使用
您可以使用FactoryBuilder类来创建您选择的HTTP工厂,或者您可以直接实例化它们。目前您的选择有guzzle
和zend
。每个工厂实现HttpFactoryInterface接口。由于这个库使用PSR-17,所有返回的各个工厂都将实现它们对应的PSR-17接口。对于更详细的使用方法,请参阅tests文件夹。
FactoryBuilder
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $zendFactory = FactoryBuilder::get('zend');
RequestFactory
由于我们可以使用guzzle
或zend
,我将在下面展示两个示例,但在其余的示例中只展示一个。但你应该知道你可以交替使用它们。
Zend
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $factory = FactoryBuilder::get('zend'); $requestFactory = $factory->requestFactory(); $r = $requestFactory->createRequest('GET', 'http://localhost');
Guzzle
你将注意到上面示例和下面示例之间的唯一区别是我们将guzzle
指定为get
方法的参数。
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $factory = FactoryBuilder::get('guzzle'); $requestFactory = $factory->requestFactory(); $r = $requestFactory->createRequest('GET', 'http://localhost');
无FactoryBuilder
您不必使用FactoryBuilder
来获取HTTP工厂。下面是一个示例。
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\GuzzleHttpFactory; $factory = new GuzzleHttpFactory(); $requestFactory = $factory->requestFactory(); $r = $requestFactory->createRequest('GET', 'http://localhost');
ResponseFactory
在其余的示例中,我将只展示一个使用zend
和FactoryBuilder
的示例。
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $factory = FactoryBuilder::get('zend'); $responseFactory = $factory->responseFactory(); $r = $responseFactory->createResponse(200, 'OK');
ServerRequestFactory
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $factory = FactoryBuilder::get('zend'); $reqFactory = $factory->serverRequestFactory(); $params = ['name' => 'value']; $r = $reqFactory->createServerRequest('GET', '/', $params);
StreamFactory
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $httpFactory = FactoryBuilder::get('zend'); $factory = $httpFactory->streamFactory(); $params = ['name' => 'value']; $r = $factory->createStream('php://memory');
UploadedFileFactory
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $httpFactory = FactoryBuilder::get('zend'); $streamFactory = $httpFactory->streamFactory(); $resource = $streamFactory->createStream('php://temp'); $factory = $httpFactory->uploadedFileFactory(); $params = ['name' => 'value']; $r = $factory->createUploadedFile( $resource, 1, \UPLOAD_ERR_OK, 'filename.txt', 'txt' );
UriFactory
<?php require 'vendor/autoload.php'; use Bulldog\HttpFactory\FactoryBuilder; $httpFactory = FactoryBuilder::get('zend'); $factory = $httpFactory->uriFactory(); $r = $factory->createUri('/');