收藏
回答

微信支付api,GuzzleHttp报错

安装:
composer require wechatpay/wechatpay
Using version ^1.4 for wechatpay/wechatpay
./composer.json has been updated
Running composer update wechatpay/wechatpay
Loading composer repositories with package information
Updating dependencies
Lock file operations: 11 installs, 0 updates, 0 removals
  - Locking guzzlehttp/guzzle (7.4.1)
  - Locking guzzlehttp/promises (1.5.1)
  - Locking guzzlehttp/psr7 (2.1.0)
  - Locking guzzlehttp/uri-template (v1.0.1)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-factory (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking ralouphie/getallheaders (3.0.3)
  - Locking symfony/deprecation-contracts (v2.5.0)
  - Locking symfony/polyfill-php80 (v1.25.0)
  - Locking wechatpay/wechatpay (1.4.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 11 installs, 0 updates, 0 removals
  - Installing guzzlehttp/promises (1.5.1): Extracting archive
  - Installing ralouphie/getallheaders (3.0.3): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-factory (1.0.1): Extracting archive
  - Installing guzzlehttp/psr7 (2.1.0): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing symfony/deprecation-contracts (v2.5.0): Extracting archive
  - Installing symfony/polyfill-php80 (v1.25.0): Extracting archive
  - Installing guzzlehttp/uri-template (v1.0.1): Extracting archive
  - Installing guzzlehttp/guzzle (7.4.1): Extracting archive
  - Installing wechatpay/wechatpay (1.4.3): Extracting archive


PHP版本:7.4.28
PHP代码:
require '..../classes/vendor/autoload.php';
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;
use WeChatPay\Util\MediaUtil;

.....

// 构造一个 APIv3 客户端实例
$instance = Builder::factory([
    'mchid'      => $merchantId,
    'serial'     => $merchantCertificateSerial,
    'privateKey' => $merchantPrivateKeyInstance,
    'certs'      => [
        $platformCertificateSerial => $platformPublicKeyInstance,
    ],
]);

// 发送请求
$resp = $instance->chain('v3/certificates')->get();


运行结果:
Fatal error:  Uncaught GuzzleHttp\Exception\ClientException: Client error: `GET https://api.mch.weixin.qq.com/v3/certificates` resulted in a `401 Unauthorized` response:
{"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_ (truncated...)
 in .../classes/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
Stack trace:
#0 .../classes/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response), NULL, Array, NULL)
#1 .../classes/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 .../classes/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), NULL)
#3 .../classes/vendor/guzzlehttp/p in .../classes/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

回答关注问题邀请回答
收藏

2 个回答

  • 乐言
    乐言
    2022-03-07

    按照文档:https://github.com/wechatpay-apiv3/wechatpay-guzzle-middleware

    “开始”部分一切正常。

    在“上传媒体文件”部分:

    // 参考上述指引说明,并引入 `MediaUtil` 正常初始化,无额外条件
    use WechatPay\GuzzleMiddleware\Util\MediaUtil;
    // 实例化一个媒体文件流,注意文件后缀名需符合接口要求
    $media = new MediaUtil('/your/file/path/with.extension');
    


    执行new MediaUtil的时候,报错:


    Fatal error:  Uncaught Error: Call to undefined function GuzzleHttp\Psr7\hash() in .../classes/vendor/wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php:91
    Stack trace:
    #0 .../classes/vendor/wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php(75): WechatPay\GuzzleMiddleware\Util\MediaUtil->composeStream()
    #1 .../classes/util/util_wxpay.class.php(43): WechatPay\GuzzleMiddleware\Util\MediaUtil->__construct('/home/www/websi...')
    #7 {main}
      thro in .../classes/vendor/wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php on line 91
    


    已解决,修改wechatpay/wechatpay-guzzle-middleware/src/Util/MediaUtil.php第91行

    原:'sha256'   => \GuzzleHttp\Psr7\hash($stream, 'sha256'),

    改:'sha256'   => \GuzzleHttp\Psr7\Utils::hash($stream, 'sha256'),




    2022-03-07
    有用 1
    回复
  • 志昂张
    志昂张
    2023-05-10

    直接修改composer包代码也不合理啊

    2023-05-10
    有用
    回复
登录 后发表内容