Guzzle和HTTPS

时间:2015-02-18 11:08:38

标签: https silex guzzle

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

2 个答案:

答案 0 :(得分:8)

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

  

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");

答案 1 :(得分:2)

我找到了一个解决方案,但不知道它为什么会起作用:

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