【问题标题】:Setting CURL Parameters for fabpot/goutte Client为 fabpot/goutte 客户端设置 CURL 参数
【发布时间】:2016-09-16 09:49:18
【问题描述】:

我正在使用 goutte (fabpot/goutte) 开发网络爬虫。当我尝试连接到 https 站点时,它会引发错误,因为该站点正在使用自签名证书。我正在尝试找到设置 curl 参数以忽略 ssl 证书是自签名的事实的方法。 按照https://github.com/FriendsOfPHP/Goutte 中的说明,我尝试了以下代码:

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

不幸的是,执行此代码时会引发以下错误:

可捕获的致命错误:传递给 GuzzleHttp\Client::request() 的参数 3 必须是数组类型,给定布尔值

不知道如何设置参数。电话是如何预期的?任何帮助将不胜感激。

【问题讨论】:

    标签: php ssl curl goutte


    【解决方案1】:

    顺便设置 curl 选项,看起来 guzzle 将键“curl”识别为配置设置,它接受与 curl 相关的配置值数组。因此,您最初尝试实现的等价物如下所示

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

    不确定它的支持程度,因为它在 guzzle 文档中的任何地方都没有说明(这样做使它看起来依赖于 CURL,我认为这不是 guzzle 的意图。因此一般超时配置条目)。

    【讨论】:

      【解决方案2】:

      我最终做了以下事情:

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

      启动 GuzzleClient 时的 'verify' => false 使其不验证证书。

      【讨论】:

        【解决方案3】:

        在最新版本的 Goutte (v4.0) 中,仅此功能有效。

        使用这个 HttpClient 接口:

        use Symfony\Component\HttpClient\HttpClient;
        

        Goutte 实例。

        $client = new \Goutte\Client(HttpClient::create(['verify_peer' => false]));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-08
          • 1970-01-01
          • 2016-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多