doujiacai4986
2015-11-23 20:42
浏览 302
已采纳

如何通过PHP发送pem证书的curl请求?

I have a php script in my Apache server that have to send a curl request to a partner's server. Partner give me a .pem file that I have to attach to every call I do to its api.

My php script is the follow:

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

It returns:

Curl Error: unable to set private key file: 'test.pem' type PEM

Consider that it sends me .pem file and says "it has no passphrase"

图片转代码服务由CSDN问答提供 功能建议

我的Apache服务器中有一个php脚本,必须向合作伙伴的服务器发送curl请求。 合作伙伴给 我是一个.pem文件,我必须附加到我对它的api做的每一个电话。

我的php脚本如下:

  $ ch = curl_init(); 
curl_setopt($ ch,CURLOPT_URL,$ url)  ; 
curl_setopt($ ch,CURLOPT_SSLCERT,“test.pem”); 
curl_setopt($ ch,CURLOPT_SSLCERTTYPE,“PEM”); 
 
 ncurl_setopt($ ch,CURLOPT_RETURNTRANSFER,True); 
curl_setopt($ ch,CURLOPT_POST,  True); 
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ json); 
 ncurl_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文件并说“它没有密码短语 “

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanlei0282 2015-11-26 12:53
    已采纳

    I think that you need to use the tmpfile() and 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); 
    

    Source: This answer here in SO helps me with similar problem.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsdsds12222 2015-12-01 11:35

    I think you're missing curl_setopt($ch, CURLOPT_CAINFO, 'test.pem'); Have a look at cURL is unable to use client certificate , in local server for more about using client certificates in curl via PHP

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题