如何在Guzzle 5中忽略无效的SSL证书错误

91

这应该是一件容易的事情。我可以找到很多关于如何在Guzzle 3中完成它的参考资料,但它们在Guzzle 5中不起作用。

目前为止我所做的:

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

当我发送请求时,会收到以下错误:

RequestException in RequestException.php line 51:
SSL CA bundle not found: false

我在谷歌上找不到有用的关于这个错误的参考资料。如果我能够访问curl选项,那么我可以尝试像这里建议的解决方案一样(它适用于Guzzle 3,因此无法使用):http://inchoo.net/dev-talk/symfony2-guzzle-ssl-self-signed-certificate/,其中相关部分为:

$req->getCurlOptions()->set(CURLOPT_SSL_VERIFYHOST, false);
$req->getCurlOptions()->set(CURLOPT_SSL_VERIFYPEER, false);

好的,显然我只是不擅长阅读文档。最终找到了这个:http://docs.guzzlephp.org/en/latest/faq.html?highlight=ssl#how-can-i-add-custom-curl-options - Gnuffo1
5个回答

103

你应该使用

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

即一个布尔值false,而不是字符串'false'。

相关文档在此: https://docs.guzzlephp.org/en/5.3/clients.html#verify

注意:一些人提供的答案适用于Guzzle 6+,如果您使用这些版本,则这些答案是好的(但原始问题明确涉及Guzzle 5)。


50
“defaults” 实际上不是 Guzzle 5 客户端配置的一部分。你应该使用以下代码:$this->client = new GuzzleClient([ 'verify' => false ]); - tixastronauta
6
文档页面略有更改,链接为:http://guzzle.readthedocs.io/en/latest/request-options.html#verify。 - bootoffav
1
澄清一下,这个答案只适用于 Guzzle 5(问题中提到的版本),它确实有“defaults”(请参阅http://docs.guzzlephp.org/en/5.3/clients.html)。 - pjcdawkins

64

请尝试使用可正常工作的更新版本:

$this->client = new GuzzleClient(['base_uri' => 'https://api.example.com/', 'verify' => false ]);

或更简单的版本:

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

测试使用版本为6.2-dev。


58

实际版本是正确的版本

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

2018年,这个不起作用:

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

12

在新的 Laravel 客户端版本中,你可以使用以下方式:

$http = new Client(['verify' => false]);

1
这是适用于较新版本的 Guzzle 的正确答案。 - M_R_K
它适用于 Guzzle 7。 - SuB

0
对于使用Laravel的人来说,以下解决方案对我很有用:HTTP Client
 use GuzzleHttp\Client;
 use Illuminate\Support\Facades\Http;

 Http::baseUrl('http://example.com')
        ->setClient(new Client(['verify' => false]))
        ->get('user/lits.htm',[
          'page_no' => '5',
          'per_page' => '25',
        ])
        ->json();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接