web-dev-qa-db-ja.com

PHPを介してPEM証明書を含むcurlリクエストを送信する方法

パートナーのサーバーにcurlリクエストを送信する必要があるphpスクリプトがApacheサーバーにあります。パートナーは、APIに対して行うすべての呼び出しに添付する必要がある.pemファイルを提供します。

私のphpスクリプトは次のとおりです。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSLCERT, "test.pem" );
curl_setopt($ch,CURLOPT_SSLCERTTYPE,"PEM");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_VERBOSE, true);

$result = curl_exec($ch);

if(!$result)
{
    echo "Curl Error: " . curl_error($ch);
}
else
{
    echo "Success: ". $result;
}

curl_close($ch);

それは返します:

カールエラー:秘密鍵ファイルを設定できません: 'test.pem'タイプPEM

それが私に.pemファイルを送信し、「パスフレーズがない」と言っていると考えてください

23
user3914418

tmpfile()stream_get_meta_data を使用する必要があると思います。

$pemFile = tmpfile();
fwrite($pemFile, "test.pem");//the path for the pem file
$tempPemPath = stream_get_meta_data($pemFile);
$tempPemPath = $tempPemPath['uri'];
curl_setopt($ch, CURLOPT_SSLCERT, $tempPemPath); 

出典: この回答はSOにあります は、同様の問題を解決するのに役立ちます。

16
James

不足していると思いますcurl_setopt($ch, CURLOPT_CAINFO, 'test.pem');を参照してください cURLはローカルサーバーでクライアント証明書を使用できません PHP経由でcurlでクライアント証明書を使用する方法の詳細

1
in need of help