Curl因响应而失败:'无法获得本地发行人证书'在mailwizz上

时间:2016-10-21 16:04:12

标签: php curl

我一直在尝试在mailwizz上开发一个支付网关,为我的国家提供支付平台,一切正常......直到我尝试使用支付网关的课程验证交易,我才得到了工作那个错误......我认为这个问题来自支付网关的课程

private function callViaCurl($interface, $payload = [ ], $sentargs = [ ])
{


    $endpoint = PaystackHelpersRouter::PAYSTACK_API_ROOT . $interface[PaystackContractsRouteInterface::ENDPOINT_KEY];
    $method = $interface[PaystackContractsRouteInterface::METHOD_KEY];

    $this->moveArgsToSentargs($interface, $payload, $sentargs);
    $this->putArgsIntoEndpoint($endpoint, $sentargs);

    $headers = ["Authorization"=>"Bearer " . $this->secret_key ];
    $body = '';
    if (($method === PaystackContractsRouteInterface::POST_METHOD)
        || ($method === PaystackContractsRouteInterface::PUT_METHOD)
    ) {
        $headers["Content-Type"] = "application/json";
        $body = json_encode($payload);
    } elseif ($method === PaystackContractsRouteInterface::GET_METHOD) {
        $endpoint = $endpoint . '?' . http_build_query($payload);
    }

    //open connection

    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, $endpoint);

    if ($method === PaystackContractsRouteInterface::POST_METHOD || $method === PaystackContractsRouteInterface::PUT_METHOD) {
        ($method === PaystackContractsRouteInterface:: POST_METHOD) && curl_setopt($ch, CURLOPT_POST, true);
        ($method === PaystackContractsRouteInterface ::PUT_METHOD) && curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    }
    //flatten the headers
    $flattened_headers = [];
    while (list($key, $value) = each($headers)) {
        $flattened_headers[] = $key . ": " . $value;
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $flattened_headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Make sure CURL_SSLVERSION_TLSv1_2 is defined as 6
    // Curl must be able to use TLSv1.2 to connect
    // to Paystack servers

    if (!defined('CURL_SSLVERSION_TLSV1_2')) {
        define('CURL_SSLVERSION_TLSV1_2', 6);
    }
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSV1_2);

    $response = curl_exec($ch);

    if (curl_errno($ch)) {   // should be 0
        // curl ended with an error
        $cerr = curl_error($ch);
        curl_close($ch);
        throw new Exception("Curl failed with response: '" . $cerr . "'.");
    }

    // Then, after your curl_exec call:
    $resp = json_decode($response);
    //close connection
    curl_close($ch);

    if (!$resp->status) {
        throw new Exception("Paystack Request failed with response: '" . $resp->message . "'.");
    }

    return $resp;


}

1 个答案:

答案 0 :(得分:1)

当cURL尝试建立加密连接但没有必要的本地证书存储来确定哪些证书可以信任时,就会发生这种情况。

有两种选择:

1 添加设置,让cURL知道在哪里找到本地证书存储:

curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem');

2 或者在CAINFO内设置php.ini,以便所有cURL操作都使用相同的证书文件,而无需每次都设置该选项:

<强>的php.ini

[curl]
; A default value for CURLOPT_CAINFO option. Must be an absolute path.
curl.cainfo = "/path/to/cacert.pem"

我使用this pem file,我认为它是Firefox内部证书存储的镜像。另请参阅this answer