plimpton / guzzle-bundle
将 Guzzle 集成到 Symfony2,附带用于 RESTful 接口的 WSSE 插件
Requires
- php: >=5.6
- eightpoints/guzzle-wsse-middleware: ~4.0
- guzzlehttp/guzzle: ~6.0
- psr/log: ~1.0
- symfony/dependency-injection: ~2.3|~3.0
- symfony/event-dispatcher: ~2.3|~3.0
- symfony/expression-language: ~2.3|~3.0
- symfony/http-kernel: ~2.3|~3.0
Requires (Dev)
- phpunit/phpunit: ~5.4
- symfony/config: 2.3|~3.0
This package is not auto-updated.
Last update: 2024-09-24 16:34:04 UTC
README
需求 | 安装 | 用法 | 事件 | 功能 | 建议 | 贡献 | 许可
Symfony GuzzleBundle
此包将 Guzzle 6.x 集成到 Symfony 中。Guzzle 是一个用于构建 RESTful Web 服务客户端的 PHP 框架。它附带一个可选的 WSSE 认证插件。
GuzzleBundle 遵循语义版本控制。更多关于 semver.org 的信息。
需求
- PHP 5.6 或更高版本
- Symfony 2.7 或更高版本
- Guzzle PHP 框架(由 composer 包含)
- WSSE 认证插件(由 composer 包含)
安装
要安装此包,运行以下命令,您将通过 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 调试工具栏/分析器
建议
添加别名:如果您想为提供的服务使用不同的名称,可以使用别名。如果您不想在服务名称中有任何对 Guzzle 的依赖,这是一个好主意。
services: crm.client: alias: guzzle.client.api_crm
贡献
👍 如果您想为此项目做出贡献,请阅读 CONTRIBUTING.md。
🎉 感谢参与此项目的 贡献者。
许可
此包在 MIT 许可 下发布。