微信小微商户图片上传接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010324331/article/details/82085474

先贴上图片上传文档地址
https://pay.weixin.qq.com/wiki/doc/api/download/img_upload.pdf

图片上传接口返回的media_id在申请入驻接口时有用,所以一开始就得把这个调通,才能继续往下走申请入驻接口。
下面直接上代码看着清楚点

<?php
    /**
     * uploadMedia.php
     *
     * Created by PhpStorm.
     * author: liuml  
     * DateTime: 2018/8/24  13:59
     */

    namespace App\Wechat\V1\Services\wechat\traits;

    use App\Wechat\V1\exception\WxException;
    use Illuminate\Support\Facades\Config;
    use Illuminate\Support\Facades\Request;

    trait UploadMedia
    {
        protected $media_addr;

        public function uploadImg()
        {
            $url = self::WXAPIHOST . 'secapi/mch/uploadmedia';
            // 判断图片地址是否为空,空的话就调用图片上传方法,把图片上传到服务器
            empty($this->media_addr) && $this->saveImg();
            // 判断图片是否存在
            if (!file_exists($this->media_addr))
                throw new WxException(10001);
            $data              = [
                'mch_id'     => $this->mch_id,
                'media_hash' => md5_file($this->media_addr),
            ];
            $data['sign_type'] = 'HMAC-SHA256';
            // 生成签名
            $data['sign']      = $this->makeSign($data, $data['sign_type']);
            // CURLFile 类的解释 http://php.net/manual/zh/class.curlfile.php
            $data['media']     = new \CURLFile($this->media_addr);
            $header            = [
                "content-type:multipart/form-data",
            ];
            $res               = $this->httpsRequest($url, $data, $header, true);
            if ($res[1] == 200) {
                $rt = $this->fromXml($res[0]);
                if ($rt['return_code'] != 'SUCCESS' && $rt['result_code'] != 'SUCCESS') {
                    throw new WxException(0, $rt['return_msg']);
                }
                if ($this->checkSign($rt)) {
                    return [
                        'media_id' => $rt['media_id'],
                    ];
                }
            }
            throw new WxException(30002);
        }

        public function saveImg()
        {
            $images = Request::file('media'); //1、使用laravel 自带的request类来获取一下文件
            if (!$images) {
                \Log::info('saveImg' . microtime(true));
                throw new WxException(0, '至少上传一张图片');
            }
            $uploadMediaAddr = Config::get('api.wechatConfig.uploadMediaAddr'); //2、定义图片上传路径
            $imagesName      = $images->getClientOriginalName(); //3、获取上传图片的文件名
            $extension       = $images->getClientOriginalExtension(); //4、获取上传图片的后缀名
            if (in_array($extension, ['jpeg', 'jpg', 'bmp', 'png'])) {
                $newImagesName = md5(microtime()) . random_int(10000, 50000) . "." . $extension;//5、重新命名上传文件名字
                $res           = $images->move($uploadMediaAddr, $newImagesName); //6、使用move方法移动文件.
                return $this->media_addr = $res->getRealPath();
            }
            throw new \Exception(10002);
        }
    }

注:这个接口需要证书,curl设置好,不然返回为空(设置如下图)
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u010324331/article/details/82085474