laravel-http-client

11.x-dev
版本
2025-01-08
版本更新時間
2334
安裝
5
star
webman-tech/laravel-http-client
Laravel illuminate/http 中的 HttpClient for webman
介紹
站在巨人(laravel)的肩膀上使文件存儲使用更加可靠和便捷
所有方法和配置與 laravel 幾乎一模一樣,因此使用方式完全參考 Laravel文檔 即可
安裝
composer require webman-tech/laravel-http-client
使用
所有 API 同 laravel,以下僅對有些特殊的操作做說明
Facade 入口
使用 WebmanTech\LaravelHttpClient\Facades\Http
代替 Illuminate\Support\Facades\Http
請求日志
配置文件 config/plugin/webman-tech/laravel-http-client/app.php
中的 log
欄目可以配置日志相關(guān)
默認(rèn)未啟用
默認(rèn)的 guzzle options 配置
配置文件 config/plugin/webman-tech/laravel-http-client/app.php
中的 guzzle
欄目可以配置 guzzle 的默認(rèn)配置
會在每次發(fā)送請求是使用該默認(rèn)值
快速定義與簡化 api 調(diào)用
如果接口請求比較多,建議通過 macros
來預(yù)定義一些接口的請求信息(比如 baseUrl、Headers 等)
配置文件 config/plugin/webman-tech/laravel-http-client/app.php
中的 macros
欄目中
舉例:
config
return [
// 其他配置省略
'macros' => [
'httpbin' => function() {
return Http::baseUrl('https://httpbin.org')
->asJson();
}
],
];
使用
$response = \WebmanTech\LaravelHttpClient\Facades\Http::httpbin()->get('get', ['abc' => 'xyz']);
建議
為了 macros 的代碼提示,建議新建一個 support/facade/Http
繼承自 WebmanTech\LaravelHttpClient\Facades\Http
,然后頂部添加注釋用于代碼提示
例如:
<?php
namespace support\facade;
use Illuminate\Http\Client\PendingRequest;
/**
* @method static PendingRequest httpbin()
*/
class Http extends \WebmanTech\LaravelHttpClient\Facades\Http
{
public static function getAllMacros(): array
{
return [
'httpbin' => function() {
return Http::baseUrl('https://httpbin.org')
->asJson();
}
];
}
}
$response = \support\facade\Http::httpbin()->get('get', ['abc' => 'xyz']);