Client.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\MicroMerchant\Media;
  11. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  12. use EasyWeChat\MicroMerchant\Kernel\BaseClient;
  13. /**
  14. * Class Client.
  15. *
  16. * @author liuml <liumenglei0211@163.com>
  17. * @DateTime 2019-06-10 14:50
  18. */
  19. class Client extends BaseClient
  20. {
  21. /**
  22. * Upload material.
  23. *
  24. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  25. *
  26. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  27. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  28. * @throws \EasyWeChat\MicroMerchant\Kernel\Exceptions\InvalidSignException
  29. */
  30. public function upload(string $path)
  31. {
  32. if (!file_exists($path) || !is_readable($path)) {
  33. throw new InvalidArgumentException(sprintf("File does not exist, or the file is unreadable: '%s'", $path));
  34. }
  35. $form = [
  36. 'media_hash' => strtolower(md5_file($path)),
  37. 'sign_type' => 'HMAC-SHA256',
  38. ];
  39. return $this->httpUpload('secapi/mch/uploadmedia', ['media' => $path], $form);
  40. }
  41. }