Skip to content

图片上传

服务商操作员调用 API 接口,上传证件照片。支持 jpeg、jpg、bmp、png 格式,图片大小不超过2M。通过 POST 表单来调用该接口,表单媒体文件 id 为 media,需有 filename、filelength、content-type 等信息。

请求参数类型描述
securitytrue声明加载商户API证书
bodyobjectmultipart/form-data 数据结构
mediaobject媒体文件标识,有filename、filelength、content-type等信息。不参与签名计算
media_hashstring根据媒体文件内容进行MD5计算后的值,注意小写
mch_idstring服务商商户号或渠道号
sign_typestringMD5 | HMAC-SHA256 枚举值之一
signstringmedia_hash mch_id sign_type 三个参数通用签名算法计算得出的签名值
php
$stack = $instance->getDriver()->select('v2')->getConfig('handler');
$stack->remove('transform_request');
$stream = new \GuzzleHttp\Psr7\LazyOpenStream('file:///path/to/image.jpg', 'rb');
$mediaMd5Digest = \GuzzleHttp\Psr7\Utils::hash($stream, 'md5');

$instance->v2->secapi->mch->uploadmedia->postAsync([
  'security' => true,
  'handler' => $stack,
  'body' => new \GuzzleHttp\Psr7\MultipartStream([
    ['name' => 'media', 'content' => $stream, 'filename' => 'image.jpg'],
    ['name' => 'mch_id', 'content' => $mchid],
    ['name' => 'media_hash', 'content' => $mediaMd5Digest],
    ['name' => 'sign_type', 'content' => $signType],
    ['name' => 'sign', 'content' => \WeChatPay\Crypto\Hash::sign(
      $signType, "mch_id=$mchid&media_hash=$mediaMd5Digest&sign_type=$signType", $apiv2Key
    )],
  ]),
])
->then(static function(\Psr\Http\Message\ResponseInterface $response) {
  print_r(\WeChatPay\Transformer::toArray((string) $response->getBody()));
})
->wait();
php
$stack = $instance->getDriver()->select('v2')->getConfig('handler');
$stack->remove('transform_request');
$stream = new \GuzzleHttp\Psr7\LazyOpenStream('file:///path/to/image.jpg', 'rb');
$mediaMd5Digest = \GuzzleHttp\Psr7\Utils::hash($stream, 'md5');

$instance->chain('v2/secapi/mch/uploadmedia')->postAsync([
  'security' => true,
  'handler' => $stack,
  'body' => new \GuzzleHttp\Psr7\MultipartStream([
    ['name' => 'media', 'content' => $stream, 'filename' => 'image.jpg'],
    ['name' => 'mch_id', 'content' => $mchid],
    ['name' => 'media_hash', 'content' => $mediaMd5Digest],
    ['name' => 'sign_type', 'content' => $signType],
    ['name' => 'sign', 'content' => \WeChatPay\Crypto\Hash::sign(
      $signType, "mch_id=$mchid&media_hash=$mediaMd5Digest&sign_type=$signType", $apiv2Key
    )],
  ]),
])
->then(static function(\Psr\Http\Message\ResponseInterface $response) {
  print_r(\WeChatPay\Transformer::toArray((string) $response->getBody()));
})
->wait();
php
$stack = $instance->getDriver()->select('v2')->getConfig('handler');
$stack->remove('transform_request');
$stream = new \GuzzleHttp\Psr7\LazyOpenStream('file:///path/to/image.jpg', 'rb');
$mediaMd5Digest = \GuzzleHttp\Psr7\Utils::hash($stream, 'md5');

$instance['v2/secapi/mch/uploadmedia']->postAsync([
  'security' => true,
  'handler' => $stack,
  'body' => new \GuzzleHttp\Psr7\MultipartStream([
    ['name' => 'media', 'content' => $stream, 'filename' => 'image.jpg'],
    ['name' => 'mch_id', 'content' => $mchid],
    ['name' => 'media_hash', 'content' => $mediaMd5Digest],
    ['name' => 'sign_type', 'content' => $signType],
    ['name' => 'sign', 'content' => \WeChatPay\Crypto\Hash::sign(
      $signType, "mch_id=$mchid&media_hash=$mediaMd5Digest&sign_type=$signType", $apiv2Key
    )],
  ]),
])
->then(static function(\Psr\Http\Message\ResponseInterface $response) {
  print_r(\WeChatPay\Transformer::toArray((string) $response->getBody()));
})
->wait();
php
$stack = $instance->getDriver()->select('v2')->getConfig('handler');
$stack->remove('transform_request');
$stream = new \GuzzleHttp\Psr7\LazyOpenStream('file:///path/to/image.jpg', 'rb');
$mediaMd5Digest = \GuzzleHttp\Psr7\Utils::hash($stream, 'md5');

$response = $instance->v2->secapi->mch->uploadmedia->post([
  'security' => true,
  'handler' => $stack,
  'body' => new \GuzzleHttp\Psr7\MultipartStream([
    ['name' => 'media', 'content' => $stream, 'filename' => 'image.jpg'],
    ['name' => 'mch_id', 'content' => $mchid],
    ['name' => 'media_hash', 'content' => $mediaMd5Digest],
    ['name' => 'sign_type', 'content' => $signType],
    ['name' => 'sign', 'content' => \WeChatPay\Crypto\Hash::sign(
      $signType, "mch_id=$mchid&media_hash=$mediaMd5Digest&sign_type=$signType", $apiv2Key
    )],
  ]),
]);
print_r(\WeChatPay\Transformer::toArray((string) $response->getBody()));
php
$stack = $instance->getDriver()->select('v2')->getConfig('handler');
$stack->remove('transform_request');
$stream = new \GuzzleHttp\Psr7\LazyOpenStream('file:///path/to/image.jpg', 'rb');
$mediaMd5Digest = \GuzzleHttp\Psr7\Utils::hash($stream, 'md5');

$response = $instance->chain('v2/secapi/mch/uploadmedia')->post([
  'security' => true,
  'handler' => $stack,
  'body' => new \GuzzleHttp\Psr7\MultipartStream([
    ['name' => 'media', 'content' => $stream, 'filename' => 'image.jpg'],
    ['name' => 'mch_id', 'content' => $mchid],
    ['name' => 'media_hash', 'content' => $mediaMd5Digest],
    ['name' => 'sign_type', 'content' => $signType],
    ['name' => 'sign', 'content' => \WeChatPay\Crypto\Hash::sign(
      $signType, "mch_id=$mchid&media_hash=$mediaMd5Digest&sign_type=$signType", $apiv2Key
    )],
  ]),
]);
print_r(\WeChatPay\Transformer::toArray((string) $response->getBody()));
php
$stack = $instance->getDriver()->select('v2')->getConfig('handler');
$stack->remove('transform_request');
$stream = new \GuzzleHttp\Psr7\LazyOpenStream('file:///path/to/image.jpg', 'rb');
$mediaMd5Digest = \GuzzleHttp\Psr7\Utils::hash($stream, 'md5');

$response = $instance['v2/secapi/mch/uploadmedia']->post([
  'security' => true,
  'handler' => $stack,
  'body' => new \GuzzleHttp\Psr7\MultipartStream([
    ['name' => 'media', 'content' => $stream, 'filename' => 'image.jpg'],
    ['name' => 'mch_id', 'content' => $mchid],
    ['name' => 'media_hash', 'content' => $mediaMd5Digest],
    ['name' => 'sign_type', 'content' => $signType],
    ['name' => 'sign', 'content' => \WeChatPay\Crypto\Hash::sign(
      $signType, "mch_id=$mchid&media_hash=$mediaMd5Digest&sign_type=$signType", $apiv2Key
    )],
  ]),
]);
print_r(\WeChatPay\Transformer::toArray((string) $response->getBody()));
返回字典类型描述
return_codestring返回状态码
SUCCESS | FAIL 枚举值之一
return_msgstring返回信息
appidstring公众账号ID
mch_idstring商户号
nonce_strstring随机字符串
signstring签名
result_codestring业务结果
SUCCESS | FAIL 枚举值之一
err_codestring错误代码
err_code_desstring错误代码描述
media_idstring媒体标识Id,微信返回的媒体文件标识Id

参阅 官方文档

Published on the GitHub by TheNorthMemory