wechatpay-apiv3 / wechatpay-php

微信支付 APIv3 的官方 PHP Library,同时也支持 APIv2
Apache License 2.0
475 stars 98 forks source link

异常的消息数据中,我能不能只要response这部分,其他多余的信息不要 #109

Closed leeboo closed 1 year ago

leeboo commented 1 year ago

Client error: POST https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/ resulted in a 400 Bad Request response: {"code":"PARAM_ERROR","message":"开户银行省市编码(/bank_account_info/bank_address_code)取值有误,请检查

我只想要 {"code":"PARAM_ERROR","message":"开户银行省市编码(/bank_account_info/bank_address_code)取值有误,请检查这部分json

怎么弄?

TheNorthMemory commented 1 year ago

两种方式:

  1. 同步模式:
try {
    $r = $instance->chain('v3/applyment4sub/applyment/')->post([]);
    $m = json_decode((string)$r->getBody());
    print_r($m);
} catch(Exception $e) {
    if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
        $r = $e->getResponse();
        $m = json_decode((string)$r->getBody());
        print_r($m);
    }
}
  1. 异步链式:
$instance->chain('v3/applyment4sub/applyment/')
->postAsync([])
->then(static function(\Psr\Http\Message\ResponseInterface $r) {
    $m = json_decode((string)$r->getBody());
    print_r($m);
})
->otherwise(static function (\GuzzleHttp\Exception\RequestException $e) {
    $r = $e->getResponse();
    $m = json_decode((string)$r->getBody());
    print_r($m);
})
->wait();