plimpton/guzzle-bundle

将 Guzzle 集成到 Symfony2,附带用于 RESTful 接口的 WSSE 插件

安装: 12

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 71

类型:symfony-bundle

v5.3.0 2016-12-30 13:35 UTC

README

需求 | 安装 | 用法 | 事件 | 功能 | 建议 | 贡献 | 许可

Symfony GuzzleBundle

Total Downloads Monthly Downloads Latest Stable Version Build Status License Dependency Status SensioLabsInsight

此包将 Guzzle 6.x 集成到 Symfony 中。Guzzle 是一个用于构建 RESTful Web 服务客户端的 PHP 框架。它附带一个可选的 WSSE 认证插件。

GuzzleBundle 遵循语义版本控制。更多关于 semver.org 的信息。

需求

安装

要安装此包,运行以下命令,您将通过 Packagist 获取最新版本。

composer require plimpton/guzzle-bundle

要使用最新版(可能是不稳定的)版本,请在您的 composer.json 中添加以下内容

{
    "require": {
        "plimpton/guzzle-bundle": "dev-master"
    }
}

用法

在 AppKernel.php 中加载包

new plimpton\Bundle\GuzzleBundle\GuzzleBundle()

在 config.yml 中配置

guzzle:
    # (de)activate logging/profiler; default: %kernel.debug%
    logging: true

    clients:
        api_payment:
            base_url: "http://api.domain.tld"

            # custom headers (@deprecated, will be removed in v6; new: "headers" in options (see below))
            headers:
                Accept: "application/json"

            # guzzle client options (full description here: https://guzzle.readthedocs.org/en/latest/request-options.html)
            # NOTE: "headers" option is not accepted here as it is provided as described above.
            options:
                auth:
                    - acme     # login
                    - pa55w0rd # password

                headers:
                    Accept: "application/json"

                timeout: 30

            # plugin settings
            plugin:
                wsse:
                    username:   "acme"
                    password:   "pa55w0rd"
                    created_at: "-10 seconds" # optional

        api_crm:
            base_url: "http://api.crm.tld"
            headers:
                Accept: "application/json"

        ...

所有这些设置都是可选的。如果定义了 WSSE 用户名,则 WSSE 插件将自动注入。

在控制器中使用服务(guzzle.client.api_crm 代表 yaml 配置中的客户端名称,是 GuzzleHttp\Client 的实例)

/** @var \GuzzleHttp\Client $client */
$client   = $this->get('guzzle.client.api_crm');
$response = $client->get('/users');

事件

处理事件。在向远程主机发送请求之前和之后会触发事件。

监听事件

    <service id="listenerID" class="Your\ListenerClass\That\Implements\GuzzleEventListenerInterface">  
        <tag name="kernel.event_listener" event="guzzle_bundle.pre_transaction" method="onPreTransaction" service="servicename"/>  
    </service>  

您的事件监听器或订阅者 必须 实现 GuzzleBundle\Events\GuzzleEventListenerInterface。
触发的事件是 guzzle_bundle.pre_transaction 和 guzzle_bundle.post_transaction。
服务标签用于确保如果您有多个 REST 端点,您可以定义特定监听器感兴趣的特定服务。

功能

Symfony 调试工具栏/分析器

Debug Logs

建议

添加别名:如果您想为提供的服务使用不同的名称,可以使用别名。如果您不想在服务名称中有任何对 Guzzle 的依赖,这是一个好主意。

services:
   crm.client:
       alias: guzzle.client.api_crm

贡献

👍 如果您想为此项目做出贡献,请阅读 CONTRIBUTING.md

🎉 感谢参与此项目的 贡献者

许可

此包在 MIT 许可 下发布。