失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 企业付款php 接口 微信企业付款接口PHP开发需要注意的两个地方

企业付款php 接口 微信企业付款接口PHP开发需要注意的两个地方

时间:2022-01-31 17:00:01

相关推荐

企业付款php 接口 微信企业付款接口PHP开发需要注意的两个地方

curl拓展

curl_setopt($ch, CURLOPT_SSLCERT, getcwd().'/cert/mycert.pem');

在做微信支付接口双向验证的时候,需要pem证书文件,然而在Mac上做本地测试的时候一直返回curl 58错误,经过一番查阅,发现,php的curl拓展在Mac和linux上是不大一样的两者的协议栈不一样

Mac下的协议栈是Secure Transport

而ubuntu下的协议栈是 openssl

(据说centos也有这个问题)

openssl函数

在获取到微信返回的rsa公钥字符串后,需要通过php的openssl_public_encrypt()函数对数据进行公钥加密,然而拿到的rsa公钥字符串却无法直接用openssl_get_publickey()来获取其中的公钥,我第一反应以为是格式(空格换行符)等原因造成的,调了一下午之后终于发现了端倪,仔细看这个获取公钥的函数,官方是这么介绍的

an X.509 certificate resource

a string having the format file://path/to/file.pem. The named file must contain a PEM encoded certificate/public key (it may contain both).

A PEM formatted public key.

复制代码注意一下这个X.509,这个函数是以这个风格加载public key的,而微信给我们的是公钥是PKCS#1 RSA public key,因此不能直接加载,总是返回false

而评论区告诉了我们思路与具体实现

思路如下

If you are trying to read a PKCS#1 RSA public key you run into trouble, because openssl wants the public key in X.509 style.

The PKCS#1 RSA public key

-----BEGIN RSA PUBLIC KEY-----

MIIBCgKCAQEAgYxTW5Yj+5QiQtlPMnS9kqQ/HVp+T2KtmvShe68cm8luR7Dampmb

[...]

cbn6n2FsV91BlEnrAKq65PGJxcwcH5+aJwIDAQAB

-----END RSA PUBLIC KEY-----

.. is not readable while the X.509 style public key

-----BEGIN PUBLIC KEY-----

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgYxTW5Yj+5QiQtlPMnS9

[..]

JwIDAQAB

-----END PUBLIC KEY-----

is. You can use an easy (and dirty) work around to read the PKCS#1 RSA anyway. The first few bytes of the X.509 style public key contain header information and can shamelessly be copied.

In other words: Delete everything after the first 32 bytes from the above X.509 key (starting behind Q8A) and attach your PKCS#1 data, reformat to 64 bytes length and use it with openssl.

Please note: The above example only works for 2048 bit length.

Like I said - it's kind of dirty - but hey - if you're as desperate as I was.

Michaela

复制代码

具体实现如下

// Michaela code retranscription can be :

$key = str_replace([

'-----BEGIN RSA PUBLIC KEY-----',

'-----END RSA PUBLIC KEY-----',

"\r\n",

"\n",

], [

'',

'',

"\n",

''

], $key);

$key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' . trim($key);

$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

复制代码当然了,如果你想像微信官方介绍的那样,让文件落地,一劳永逸,可以用openssl rsa -RSAPublicKey_in -in -pubout将PKCS#1标准的RSA公钥pem文件转换为PKCS#8标准的文件,道理是一样的

如果觉得《企业付款php 接口 微信企业付款接口PHP开发需要注意的两个地方》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。