如何使用这个sdk上传图片。我是用如下代码,无法通过验签
$wechatPayMiddleware = WechatPayMiddleware::builder()
->withMerchant(CommonData::MCH_ID, CommonData::SERIAL_NUMBER, CommonData::getPrivateKey())
->withWechatPay([ CommonData::getCertificate() ]) // 可传入多个微信支付平台证书,参数类型为array
->withValidator(new NoValidator())
->build();
// 将WechatPayMiddleware添加到Guzzle的HandlerStack中
$stack = HandlerStack::create();
$stack->push($wechatPayMiddleware, 'wechatpay');
// 创建Guzzle HTTP Client时,将HandlerStack传入
$client = new \GuzzleHttp\Client(['handler' => $stack, 'verify' => false]);
$url = 'https://xxx.com/'.$object;
// 接下来,正常使用Guzzle发起API请求,WechatPayMiddleware会自动地处理签名和验签
$resp = $client->request('POST', CommonData::URL . $this->getUri(), [
'headers' => [ 'Accept' => 'application/json' ],
'multipart' => [
[
'name'=>'file',
'contents'=>fopen($url, 'r')
], [
'name'=>'meta',
'contents'=>json_encode([
'filename'=>basename($url),
'sha256'=>hash('sha256', (new Oss())->downloadFile('hic-image', $object)),
])
]
],
]);
$result = json_decode($resp->getBody()->getContents(), true);
https://github.com/wechatpay-apiv3/wechatpay-guzzle-middleware/pull/16 已0侵入解决图片上传问题,不用再花精力拼接body体了,用法见 commit 说明。
use WechatPay\GuzzleMiddleware\Util\MediaUtil; // 实例化一个媒体文件流,注意文件后缀名需符合接口要求 $media = new MediaUtil('/your/file/path/with.extension'); // POST 语法糖 $resp = $client->post('merchant/media/upload', [ 'body' => $media->getStream(), 'headers' => [ 'Accept' => 'application/json', 'content-type' => $media->getContentType(), ] ]);
你这边具体调用的是哪个接口?验签过不了,一般是你的签名参数有问题,仔细核对参数。