wechatpay-apiv3/wechatpay-php

输入请求参数xml格式错误

cpken opened this issue · 2 comments

cpken commented

输入请求参数xml格式错误?

请求网址 https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey

$url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
$nonce_str = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS";
$options = [
    'form_params' =>
        'xml'   => [
              'mch_id' => "123456",
              'nonce_str' => $nonce_str,
              'sign' => $nonce_str
          ]
];
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $url, $options);
$stream   = \GuzzleHttp\Psr7\Utils::streamFor($response->getBody());
$content  = $stream->getContents();
dump($content);

输出内容

<xml>
  <return_code><![CDATA[FAIL]]></return_code>
  <retmsg><![CDATA[输入请求参数xml格式错误]]></retmsg>
  <retcode><![CDATA[1]]></retcode>
</xml>

请问这是什么原因?

原因是: 你使用的是标准guzzle能力,而Guzzle6/7 已不再原生支持发送xmlcontext

sdk通过注册transformRequest中间件栈,入参php array('xml' =>[]) 转换,源码见这里:

/**
* APIv2's transformRequest, did the `datasign` and `array2xml` together
*
* @param ?string $mchid - The merchant ID
* @param string $secret - The secret key string (optional)
* @param array{cert?: ?string, key?: ?string} $merchant - The merchant private key and certificate array. (optional)
*
* @return callable(callable(RequestInterface, array))
* @throws \WeChatPay\Exception\InvalidArgumentException
*/
public static function transformRequest(?string $mchid = null, string $secret = '', ?array $merchant = null): callable

cpken commented

已解决,谢谢。