lde/api-helper

Laravel 包,帮助轻松消费 REST 和 XML API。


README


Logo

Api Helper 包

一个用于平滑消费 API 的包
探索文档 »

查看包 · 报告错误 · 请求功能

目录

入门

本包有助于消费 API,以下是安装和使用说明。

安装

  1. 使用 Packagist 安装此包

  2. 在项目根目录运行以下命令

     composer require lde/api-helper
    
  3. 此命令将安装包含依赖项的包

配置

  • 要使用此 apihelper,需要将配置文件导出到配置文件夹,请在您的终端中运行以下命令以发布配置文件。

      php artisan vendor:publish  --provider="Lde\ApiHelper\ApiHelperServiceProvider"
    
  • 这将把名为 api_helper.php 的配置文件发布到配置文件夹。

Prometheus 配置

'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 对象。

响应