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

如何通过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"

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失