背景
使用PHP
对接美团企业版接口,美团没有提供PHP
版的SDK,只提供了Java
版的SDK,提供的RSA私钥格式是base64
,PHP
不能直接使用,需要转换成pem
格式才能使用。
解决办法
- 使用
PHP
先将私钥进行base64
解码然后保存到private.der
文件
$appPrivateKey = "xxxxx"
$appPrivateKey = base64_decode($appPrivaateKey);
file_put_contents('private.der', $appPrivateKey);
- 使用
OpenSSL
命令将private.der
转成private.pem
openssl rsa -inform DER -outform PEM -in private.der -out private.pem
- 使用
PHP
读取私钥并使用
$appPrivateKey = openssl_pkey_get_private(file_get_contents('private.pem'));