improwised / api-helper
Laravel 包,用于轻松消费 REST 和 XML API。
0.1.31
2021-03-24 11:33 UTC
Requires
- php: >=7.0.0
- adbario/php-dot-notation: ^2.2.0
- guzzlehttp/guzzle: ^6.3
- spatie/array-to-xml: ^2.8
Requires (Dev)
- orchestra/testbench: ~3.6.0|~3.7.0|~3.8.0
- phpunit/phpunit: ^7.0|^8.0
- dev-master
- 0.1.31
- 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/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-12 09:38:39 UTC
README
Api Helper 包
一个用于平滑消费 API 的包
探索文档
查看包 · 报告错误 · 请求功能
目录
入门
此包用于消费 API,以下是安装和使用说明。
安装
-
要使用 Packagist 安装此包
-
在项目的根目录下运行以下命令
composer require improwised/api-helper
-
此命令将安装包含依赖的包
配置
-
要使用 apihelper,需要导出配置文件,为此请在您的终端中运行以下命令以将配置文件发布到配置文件夹。
php artisan vendor:publish --provider="Improwised\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 标签。 - 您可以在 Prometheus 配置中设置
histogram_bucket
为数组。
使用
-
要使用此包,您需要在想使用此包的地方添加以下类。
use Improwised\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