nnjeim / fetch
Laravel 对 Guzzle 客户端库的封装
1.1.1
2021-10-11 08:12 UTC
Requires
- php: >=7.4
- guzzlehttp/guzzle: >=7.0.0
Requires (Dev)
- orchestra/testbench: >=v4.0.0
- phpunit/phpunit: >=8.5.8
This package is auto-updated.
Last update: 2024-09-05 19:09:43 UTC
README
Nnjeim Guzzle 包装器
这是一个 Laravel 对 Guzzle 客户端 HTTP 库的封装。它提供了流畅的语法来发送 HTTP 请求。
安装
您可以通过 composer 安装此包
composer require nnjeim/fetch
配置
php artisan vendor:publish --provider="Nnjeim\Fetch\FetchServiceProvider"
用法
Fetch Facade
use Nnjeim\Fetch\Fetch;
$countries = Fetch::setBaseUri('https://someapi.com')->get('countries');
FetchHelper 实例化
use Nnjeim\Fetch\FetchHelper;
private $fetch;
public function __construct(FetchHelper $fetch) {
$this->fetch = $fetch;
}
.
.
.
return $this->fetch
->setBaseUri('https://someapi.com')
->get('countries');
方法
设置头部
Set the http headers
@return $this setHeaders(array $headers)
设置基本 URI
Sets the base uri for the composition of the http request url.
@return $this setBaseUri(string 'http://someapi.com/')
设置请求 URL
Sets the http request url.
@return $this setUrl(string $url)
设置请求方法
Sets the http request method.
@return $this setMethod(string $method)
设置请求体格式
Sets the request body format. The required format are 'query' | 'form_params' | 'multipart'.
@return $this setBodyFormat(string $format)
异步请求
Sets the type of the request to async.
@return $this setAsync()
GET 请求
Sets the body format to query.
@return array get(?string $url = null, ?array $data = null)
POST 请求
Sets the body format to form-params.
@return array post(?string $url = null, ?array $data = null)
PUT 请求
Sets the body format to form-params.
@return array put(?string $url = null, ?array $data = null)
DELETE 请求
Sets the body format to query.
@return array delete(?string $url = null, ?array $data = null)
上传请求
Sets the body format to multipart.
@return array upload(?string $url = null, ?array $data = null)
响应
@return array
[
'response' => ...,
'status' => ...,
];
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG