Setting CURL Parameters for fabpot/goutte Client

大憨熊 提交于 2019-12-07 08:30:38

问题


I am working on a web crowler using goutte (fabpot/goutte). When I try to connect to an https site, it throws an error because the site is using a self signed certificate. I am trying to find the way to set the curl parameters to ignore the fact that the ssl certificate is self signed. Following the instructions in https://github.com/FriendsOfPHP/Goutte I tried the following code:

    $this->client = new Client();
    $this->client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYPEER, false);
    $this->client->getClient()->setDefaultOption('config/curl/'.CURLOPT_CERTINFO, false);

Unfortunately when this code is executed the following error is thrown:

Catchable fatal error: Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, boolean given

Can't figure out how to set up the parameters. How is the call expected? Any help will be appreciated.


回答1:


To set curl options by the way, it looks like guzzle recognizes the key "curl" as a config setting, which takes in an array of curl-related config values. So the equivalent of what you were initially trying to achieve would look like the following

$client = new \Goutte\Client();

$guzzleClient = new \GuzzleHttp\Client(array(
    'curl' => array(
        CURLOPT_TIMEOUT => 60,
    ),
));
$client->setClient($guzzleClient);
$crawler = $client->request('GET', $my_url);

Not sure how well this is supported since it isn't indicated anywhere in the guzzle docs (and doing it this way makes it look like its dependent on CURL, which I think is not the intention of guzzle. Hence the general timeout config entry).




回答2:


What I ended up doing is the following:

$this->client->setClient(new GuzzleClient(['verify' => false]));

The 'verify' => false when initiating the GuzzleClient makes it not verify the certificates.



来源:https://stackoverflow.com/questions/37324500/setting-curl-parameters-for-fabpot-goutte-client

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!