微信支付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