【问题标题】:Setting CURL option via Service Container for Guzzle通过服务容器为 Guzzle 设置 CURL 选项
【发布时间】:2014-06-05 17:24:33
【问题描述】:

我需要设置CURLOPT_TCP_NODELAY CURL 选项,但问题是我不知道如何使用 Sf2 的服务容器。

下面是Guzzle 现在的注入方式:

services:
    user.user_manager:
        class: Foo\UserBundle\Model\UserManager
        arguments:
            - @guzzle.client

但我还需要添加CURLOPT_TCP_NODELAY

简单的 PHP 示例:

$guzzle = new \Guzzle\Http\Client(null, array(
    'curl.options' => array(
        'CURLOPT_TCP_NODELAY' => 1
)));

【问题讨论】:

    标签: symfony curl guzzle


    【解决方案1】:

    您可以创建自定义 Guzzle 客户端并将其声明为服务:

    <?php
    
    namespace You\ProjectBundle\Guzzle;
    
    class MyGuzzleClient extends \Guzzle\Http\Client
    {
        public function __construct()
        {
            parent::__construct(null, array(
                'curl.options' => array('CURLOPT_TCP_NODELAY' => 1)
            ));
        }
    }
    

    然后将其声明为服务:

    services:
        my_guzzle.client:
            class: You\ProjectBundle\Guzzle\MyGuzzleClient
    

    最后按如下方式使用:

    services:
        user.user_manager:
            class: Foo\UserBundle\Model\UserManager
            arguments:
                - @my_guzzle.client
    

    【讨论】:

    • 谢谢!我想可能还有其他方法。可能使用表达式?
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2011-08-25
    相关资源
    最近更新 更多