通过服务容器为Cuzzle设置CURL选项

Rom*_*aza 0 curl symfony guzzle

我需要设置CURLOPT_TCP_NODELAYCURL选项,但是问题是我不知道如何使用Sf2的服务容器来做到这一点。

现在是如何Guzzle注入的:

services:
    user.user_manager:
        class: Foo\UserBundle\Model\UserManager
        arguments:
            - @guzzle.client
Run Code Online (Sandbox Code Playgroud)

但是我也需要添加CURLOPT_TCP_NODELAY

普通的PHP示例:

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

VBe*_*Bee 5

您可以创建一个自定义的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)
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将其声明为服务:

services:
    my_guzzle.client:
        class: You\ProjectBundle\Guzzle\MyGuzzleClient
Run Code Online (Sandbox Code Playgroud)

最后,如下使用它:

services:
    user.user_manager:
        class: Foo\UserBundle\Model\UserManager
        arguments:
            - @my_guzzle.client
Run Code Online (Sandbox Code Playgroud)