codeinc/cloudrun-auth-http-client

基于 Guzzle 的 PSR-7 HTTP 客户端,用于 Google Cloud Platform Cloud Run 上的服务

v1.4 2024-02-17 01:00 UTC

This package is auto-updated.

Last update: 2024-09-17 02:17:08 UTC


README

PHP 8.2+ 库是基于 PSR-7 的 HTTP 客户端,基于 Guzzle 和官方 Google Auth PHP 包,用于 Google Cloud Platform Cloud Run 上的服务。

安装

推荐通过 Composer 安装库

composer require codeinc/cloudrun-auth-http-client

用法

需要 GCP 服务帐户来验证发送到 Cloud Run 服务的请求。如果您还没有服务帐户,请按照此页面上的说明创建一个。

在使用库之前,服务帐户必须按照 这些步骤 验证以向 Cloud Run 服务发送请求。

use CodeInc\CloudRunAuthHttpClient\HttpClientFactory;

// create a new HttpClientFactory instance
$factory = new HttpClientFactory();

// create the client using a JSON file for the service account key
$httpClient = $factory->factory(
    // Cloud Run service URL
    'https://my-service-12345-uc.a.run.app',
    // path to your service account key 
    '/path/to/your/service-account-key.json' 
);

// create the client using a key stored in memory
$httpClient = $factory->factory(
    // Cloud Run service URL
    'https://my-service-12345-uc.a.run.app',
    // service account key 
    [
        'type' => 'service_account',
        // the rest of the service account key
    ]
);

// create the client using a key stored in a environment variable
$httpClient = $factory->factory(
    // Cloud Run service URL
    'https://my-service-12345-uc.a.run.app',
    // service account key 
    json_decode(getenv('SERVICE_ACCOUNT_KEY'), true)
);

许可协议

该库在 MIT 许可协议下发布(参见 LICENSE 文件)。