【问题标题】:Guzzle and HTTPSGuzzle 和 HTTPS
【发布时间】:2015-04-19 08:44:32
【问题描述】:

我想使用 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

【问题讨论】:

    标签: https silex guzzle


    【解决方案1】:

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

    cURL 错误 60:见 http://curl.haxx.se/libcurl/c/libcurl-errors.html

    查找http://curl.haxx.se/libcurl/c/libcurl-errors.html我找到了

    CURLE_SSL_CACERT (60)

    对等证书无法使用已知 CA 证书进行身份验证。

    所以这很可能是 SSL 验证/CA 捆绑包的问题。通过将verify 请求选项设置为false,guzzle(resp. curl)将不会尝试根据证书验证主机,因此错误消失(--回复https://stackoverflow.com/a/28582692/413531

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

    IIRC,在 v4 guzzle 中提供了一个默认证书(请参阅 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 时显式设置证书更容易。这意味着:

    示例(假设您有名为 cacert.pem 的证书与脚本位于同一目录中):

    $default = ["verify" => __DIR__ . "/cacert.pem"];
    $config = ["defaults" => $default];
    $client = new Client($config);
    $response = $client->get("https://www.facebook.com");
    

    【讨论】:

    • 是的,你是对的:看看这个班级的 cmets:GuzzleHttp\Message\MessageFactoryInterface
    【解决方案2】:

    我找到了解决方案,但不知道为什么可行:

    $client->setDefaultOption('verify', false);
    $response = $client->get("https://www.facebook.com");
    

    【讨论】:

    • 在验证选项中指定证书的 PEM 文件。 $client = new \GuzzleHttp\Client(['verify' => '/full/path/to/cert.pem']);
    猜你喜欢
    • 2017-02-10
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2019-02-05
    • 2015-01-19
    相关资源
    最近更新 更多