PHPUnit 上下文中的 Guzzle curl 错误 51

Guzzle curl error 51 in PHPUnit context

我有一个 Laravel 应用程序,它使用 Web 服务来验证字段,REST 调用通过 HTTPS 通过 Guzzle。当我通过浏览器 post 表单时,一切正常。当我 运行 通过 php 单元作为测试时,Guzzle 失败并显示:

exception 'GuzzleHttp\Exception\RequestException' with message 'cURL error 51: SSL: certificate verification failed (result: 5)

我试过的:

1- 在 Guzzle 之前,我使用了直卷曲,它给出了相同的行为。我尝试在代码中使用 curl_setopt($curl, CURLOPT_CAINFO, '/path/to/cacert.pem'); 显式设置 cacert,但什么也没做

2- 我尝试(相同的结果)使用

将 Guzzle 指向 cacert
$response = $client->request('GET', $url, [
    'auth' => ['user', 'pass'],
    'verify' => '/path/to/cacert.pem'
]);

3- phpinfo 说 Apache 正在使用它的本地 curl 而命令行 php 使用不同的所以我符号链接到 Apache 版本,相同的结果

4- curl.cainfo 和 openssl.cainfo 都已设置

5- /usr/local/etc/openssl/cert.pem 存在

6- 在命令行上将 -d openssl.cainfo 传递给 phpunit 什么都不做

代码:

private function curl_rest_call($url){

    // $curl = curl_init() ;

    // curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    // curl_setopt($curl, CURLOPT_USERPWD, "user:pass");

    // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    // curl_setopt($curl, CURLOPT_CAINFO, '/path/to/cacert.pem');

    // curl_setopt($curl, CURLOPT_URL, $url);
    // curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    // $rest = curl_exec($curl);
    // $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    // curl_close($curl);
    // $result = json_decode($rest) ;

    // $result->status = $httpStatus ;

    // return $result ;

    $client = new Guzzle();
    $response = $client->request('GET', $url, [
        'auth' => ['user', 'pass'],
        'verify' => '/path/to/cacert.pem'
    ]);

    $result = json_decode($response->getBody()->getContents()) ;
    $result->status = $response->getStatusCode() ;

    return $result ;

}

测试:

public function testSuccessfulSignup(){

    $this->visit('/free-software')
        ->type('test@mail.com', 'mail')
        ->type('Philip', 'first_name')
        ->type('Fry', 'last_name')
        ->select('Freelancer', 'profile')
        ->type('Other', 'industry')
        ->select('Canada', 'country')
        ->type('(514) 278-8666', 'phone_number')
        ->press('Try Harmony Now!')
        ->seePageIs('/success');
}

这是问题所在。

我从命令行调用 phpunit,因为它是一个 php 脚本而不是二进制文件,它会调用 php 的默认系统安装,该位置与安装的位置不同网络服务器正在使用,因此忽略了 cacert 指令和 php.ini config.

虽然理论上不应该,但它完全忽略了 -d openssl.cainfo 开关和 CURL_CA_BUNDLE 环境变量(curl 使用)

最后,我在 PATH 变量上添加了网络服务器 php 版本,以便调用 phpunit 将加载正确的 cacert 信息。