Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Allow to override Curl options passed to Guzzle request #69

Closed
antonbabenko opened this issue Jul 10, 2012 · 14 comments
Closed

Allow to override Curl options passed to Guzzle request #69

antonbabenko opened this issue Jul 10, 2012 · 14 comments

Comments

@antonbabenko
Copy link

Currently curl options are hardcoded in Client.php, but sometimes more options have to be set (increase timeout or max. redirects).

Code from Client.php:

 $guzzleRequest->getCurlOptions()
        ->set(CURLOPT_FOLLOWLOCATION, false)
        ->set(CURLOPT_MAXREDIRS, 0)
        ->set(CURLOPT_TIMEOUT, 30);
@mtdowling
Copy link
Contributor

You can add custom curl options to clients when injecting a Guzzle client into a Goutte client. If you prefix a client configuration option name with curl. it will be treated as a Curl option and added to each request created by the client.

See http://guzzlephp.org/tour/http.html#low-level-curl-access

@antonbabenko
Copy link
Author

@mtdowling - Thanks. It works :)

@seejohnrun
Copy link

Note: This is no longer the correct answer for redirects (and is seemingly unreliable now), there is now redirect.disable, which you can set to true:
http://guzzlephp.org/tour/http.html#low-level-curl-access

@bkuhl
Copy link

bkuhl commented Jan 16, 2014

Unfortunately both of these links are no longer valid.

@antonbabenko
Copy link
Author

@hgraca
Copy link

hgraca commented Jun 2, 2014

It's not there any more, can you find the correct link? tkx.

@antonbabenko
Copy link
Author

@jcroll
Copy link
Contributor

jcroll commented Jun 5, 2014

If you are using a recent release of "fabpot/goutte" which is using "guzzlehttp/guzzle": "4.*" I believe you can alter the curl options like below:

$client = new \Goutte\Client();
$client->getClient()->setDefaultOption('config', ['curl' => ['CURLOPT_TIMEOUT' => 60]]);
$crawler = $client->request('GET', $url);

@hgraca
Copy link

hgraca commented Jun 6, 2014

I was trying to use codeception to test my api call which can take a long time and I wanted to put the timeout to 0.
I ended up doing this:

private function setCurlOption($curlOption, $value)
{
    if (!isset($this->client)) {
        if (isset($this->config['url']) && !strpos($this->config['url'], '://')) {
            // not valid url
            foreach ($this->getModules() as $module) {
                if ($module instanceof \Codeception\Util\Framework) {
                    $this->client = $module->client;
                    break;
                }
            }
        } else {
            if (!$this->hasModule('PhpBrowser')) {
                throw new \Exception(__CLASS__, "For REST testing via HTTP please enable PhpBrowser module");
            }
            $this->client = $this->getModule('PhpBrowser')->session->getDriver()->getClient();
        }
        if (!isset($this->client)) {
            throw new \Exception(__CLASS__, "Client for REST requests not initialized.\nProvide either PhpBrowser module, or a framework module which shares FrameworkInterface");
        }
    }

    if (method_exists($this->client, 'getClient')) {
        /** @var \Guzzle\Common\Collection $clientConfig */
        $clientConfig             = $this->client->getClient()->getConfig();
        $curlOptions              = $clientConfig->get('curl.options');
        $curlOptions[$curlOption] = $value;
        $clientConfig->set('curl.options', $curlOptions);
    }
}

I got most of this from somewhere else though.

@ssanders
Copy link

For Goutte 1 / Guzzle 3 / PHP 5.3:

$client->getClient()->getConfig()->set('curl.options', array(CURLOPT_TIMEOUT => 30));

@fabpot
Copy link
Member

fabpot commented Jun 17, 2014

@ssanders Could you add a note in the README file?

@mtdowling
Copy link
Contributor

That's not how it works in Guzzle 4. See: http://docs.guzzlephp.org/en/latest/faq.html#how-can-i-add-custom-curl-options and http://docs.guzzlephp.org/en/latest/clients.html#creating-a-client.

This could be set after a client is created using:

$client->getClient()->setDefaultOption('config/curl/' . CURLOPT_XYZ, 'Foo');

There is no need to set things like timeout as that can be handled by Guzzle for various adapters using request options: http://docs.guzzlephp.org/en/latest/clients.html#timeout

fabpot added a commit that referenced this issue Jul 8, 2014
This PR was merged into the 2.0-dev branch.

Discussion
----------

[RFC] Update README.rst for Guzzle 4 cURL options

Please see:

#69

#162

Commits
-------

ed80a44 Update README.rst
@ssanders
Copy link

I don’t think that actually works in Goutte 2.

I set CURLOPT_TIMEOUT and _CONNECTTIMEOUT to 0 yet get cURL error 28 on a URL.

Guzzle itself, which I think has no default timeout, works OK, no options changed.

@YuvrajHinger
Copy link

YuvrajHinger commented Sep 15, 2020

Modify your scrapping url.
I did in a way like
http://www.yuvrajhinger.in
for every domain add http://www.

It works for me.

Hope it helps you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants