smokills/laravel-http-client-default-options

扩展原生Laravel Http Client,以便您可以定义全局选项

v2.0.2 2021-10-25 16:07 UTC

This package is auto-updated.

Last update: 2024-09-25 22:42:49 UTC


README

Image of package

Latest Version on Packagist Build Status Total Downloads License

为Laravel Http Client设置默认可用选项。

安装

通过composer安装此包

composer require smokills/laravel-http-client-default-options

Laravel

在Laravel环境中,由于Laravel Package Auto-Discovery,该包将被自动注册

Lumen

如果您想在一个Lumen安装中使用此包,您需要在 app/bootstrap.php 中注册Service Provider

$app->register(Smokills\Http\ServiceProvider::class);

使用

您可以通过以下方式为Http客户端定义全局选项

// In a boot Service provider method (ex: the AppServiceProvider)

public function boot()
{
    ...

    Http::withDefaultOptions([
        'base_uri' => 'https://foo.com',
        'headers' => [
            'X-Bar-Header' => 'bar'
        ],
    ]);
}

从现在开始,所有后续请求都将使用我们提供的默认选项

// Somewhere in the code

/**
 * Since we have defined the base_uri as default option, we can simply make a
 * request using only the uri.
 */
$response = Http::get('/baz');

如果我们需要,我们仍然可以继续添加其他选项或助手

// Somewhere in the code

/**
 * The debug option and the basic auth will be used together the default options defined before.
 */
$response = Http::withOptions([
    'debug' => 'true'
])->withBasicAuth('username', 'password')->get('/baz');

如果您需要删除多个甚至全部默认选项,以便进行其他请求,您可以使用 withoutDefaultOptions 方法。

// Remove all of the default options...
$response = Http::withoutDefaultOptions()->get('https://bar.com');

// Remove some of the global options
$response = Http::withoutDefaultOptions([
    'option', 'another-option'
])->get('https://bar.com');

// You can pass options to remove as arguments as well
$response = Http::withoutDefaultOptions('option', 'another-option')->get('https://bar.com');

// If you would like to remove deeply nested options, you may use the the dot notation syntax
$response = Http::withoutDefaultOptions('header.X-Some-Header')->get('https://bar.com');

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

鸣谢

许可

MIT许可(MIT)。请参阅 许可文件 了解更多信息。

Laravel Package Boilerplate

此包是用 Laravel Package Boilerplate 生成的。