lde/api-helper
Laravel 包,帮助轻松消费 REST 和 XML API。
8.0.0
2022-03-04 10:58 UTC
Requires
- php: ^7.4|^8.0
- adbario/php-dot-notation: ^2.x.dev
- guzzlehttp/guzzle: ^7.0.1
- spatie/array-to-xml: ^2.16.0
Requires (Dev)
- orchestra/testbench: ~3.6.0|~3.7.0|~3.8.0|~5.0.0
- phpunit/phpunit: ^8.5|9.0|^9.5
- dev-master
- 8.0.0
- 7.0.x-dev
- 7.0.0
- 6.0.0
- 0.1.30
- 0.1.29
- 0.1.28
- 0.1.27
- 0.1.26
- 0.1.25
- 0.1.24
- 0.1.23
- 0.1.22
- 0.1.21
- 0.1.20
- 0.1.19
- 0.1.18
- 0.1.17
- 0.1.16
- 0.1.15
- 0.1.14
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.0
- dev-feature/changelog
- dev-feature/checking_testcase_failure
- dev-testcaseformparams
- dev-formdatasupport
- dev-revert-25-dependabot/composer/symfony/http-foundation-4.4.1
This package is auto-updated.
Last update: 2024-09-04 16:24:29 UTC
README
Api Helper 包
一个用于平滑消费 API 的包
探索文档 »
查看包 · 报告错误 · 请求功能
目录
入门
本包有助于消费 API,以下是安装和使用说明。
安装
-
使用 Packagist 安装此包
-
在项目根目录运行以下命令
composer require lde/api-helper
-
此命令将安装包含依赖项的包
配置
-
要使用此 apihelper,需要将配置文件导出到配置文件夹,请在您的终端中运行以下命令以发布配置文件。
php artisan vendor:publish --provider="Lde\ApiHelper\ApiHelperServiceProvider"
-
这将把名为 api_helper.php 的配置文件发布到配置文件夹。
Prometheus 配置
-
Prometheus 依赖于您的应用程序,因此您需要提供 Prometheus 配置,并在您的应用程序中使用以下包。
-
如果您想使用 Prometheus,则应从配置 api_helper 中启用它。
'log_stats' => true, // If you want to use prometheus then set as true otherwise false 'prometheus' => [ 'labels' => [ 'client_id' => 10, 'app' => 'api-helper', 'source' => 'core', ], 'histogram_bucket' => [0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 7.5, 10.0], ],
- 您可以根据需要在内
prometheus.labels
中配置 Prometheus 的标签。 histogram_bucket
可以在 Prometheus 配置中设置为数组。
使用
-
要使用此包,您需要在想要使用此包的地方添加以下类。
use Lde\ApiHelper\ApiBuilder;
方法
addHeaders($headers)
-
此方法用于添加头信息。
-
它接受名称和值作为参数,在这里您可以一次设置一个头信息。
$headers['Accept'] = "application/json"; $headers['Content-Type'] = "application/json"; app(ApiBuilder::class)->addHeaders($headers);
-
我们将以 ApiBuilder 对象的形式获取响应。
api($connection)
-
此方法用于设置从 api_helper.php 中将要使用的 API,其中已经定义了 httpbin 和 mokbin,因此您需要传递您想要使用的名称。
-
您还可以在配置文件 api_helper.php 中定义自己的 API 端点。
app(ApiBuilder::class)->api('httpbin')->method_to_call();
-
该代码片段指示您如何连接特定的 API 并访问其方法。
-
method_to_call() 是您在 api_helper 连接数组中指定的函数。
-
这将返回 ApiResponse 对象。
响应
- 在这里,您将获得响应对象,在每个响应中,您将获得成功(true 或 false)。
- 您还将获得状态码以获取有关响应的更多信息,请参阅以下文档。
- http://docs.guzzlephp.org/en/latest/psr7.html#responses