gpt4 book ai didi

https - Guzzle 和HTTPS

转载 作者:行者123 更新时间:2023-12-03 13:48:50 30 4
gpt4 key购买 nike

我想使用Guzzle和Silex将请求发送到https页面。

使用http url我有一个回应:

app->get('/',function() use ($app, $client){

$response = $client->get("http://www.google.fr");

var_dump($response);

});

我的回复:
object(GuzzleHttp\Message\Response)[102]
private 'reasonPhrase' => string 'OK' (length=2)
private 'statusCode' => int 200
private 'effectiveUrl' => string 'http://www.google.fr' (length=20)
private 'headers' (GuzzleHttp\Message\AbstractMessage) =>
array (size=13)
'date' =>
array (size=1)
0 => string 'Wed, 18 Feb 2015 10:57:37 GMT' (length=29)
'expires' =>

但是用https:
$app->get('/',function() use ($app, $client){
$url = "https://api.zoapp.com/v1/stc/cans/directory/pub/Employees";

$response = $client->get("https://www.facebook.com/");

var_dump($response);

});

我要出错:
RequestException in RequestException.php line 51:


RingException in CurlFactory.php line 126:

详细信息: pastbin link

最佳答案

在链接到详细信息之后,异常消息显示:

cURL error 60: See http://curl.haxx.se/libcurl/c/libcurl-errors.html



查找 http://curl.haxx.se/libcurl/c/libcurl-errors.html我发现

CURLE_SSL_CACERT (60)

Peer certificate cannot be authenticated with known CA certificates.



因此,最有可能是SSL验证/CA捆绑包存在问题。通过将 verify request选项设置为 false,guzzle(resp。curl)将不会尝试根据证书来验证主机,因此错误消失了(-代表 https://stackoverflow.com/a/28582692/413531)

但是,您不希望这样做;)相反,您应该尝试通过提供有效的CA捆绑包来解决此问题。

IIRC在第4版中提供了默认证书(请参阅 https://github.com/guzzle/guzzle/blob/4.2.3/src/cacert.pem),但在第5版中将其删除,现在尝试发现您的默认系统CA捆绑包。从 the docs中,检查以下位置:
Check if openssl.cafile is set in your php.ini file.
Check if curl.cainfo is set in your php.ini file.
Check if /etc/pki/tls/certs/ca-bundle.crt exists (Red Hat, CentOS, Fedora; provided by the ca-certificates package)
Check if /etc/ssl/certs/ca-certificates.crt exists (Ubuntu, Debian; provided by the ca-certificates package)
Check if /usr/local/share/certs/ca-root-nss.crt exists (FreeBSD; provided by the ca_root_nss package)
Check if /usr/local/etc/openssl/cert.pem (OS X; provided by homebrew)
Check if C:\windows\system32\curl-ca-bundle.crt exists (Windows)
Check if C:\windows\curl-ca-bundle.crt exists (Windows)

但是,我发现在创建新的 Client时更容易显式设置证书。这意味着:
  • 下载https://github.com/guzzle/guzzle/blob/4.2.3/src/cacert.pem或更高版本(因为更新)http://curl.haxx.se/ca/cacert.pem(请参阅http://curl.haxx.se/docs/caextract.html)
  • Client实例化
  • 中使用此证书的本地路径

    示例(假设您具有与脚本位于同一目录中的名为 cacert.pem的证书):
    $default = ["verify" => __DIR__ . "/cacert.pem"];
    $config = ["defaults" => $default];
    $client = new Client($config);
    $response = $client->get("https://www.facebook.com");

    关于https - Guzzle 和HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28582091/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com