微信获取下载临时素材(图片)

<?php

namespace App\Http\Controllers\User;

use Illuminate\Support\Facades\Storage;

/**
 *Time 2022/3/15
 */

class GetWCApi
{
    private $grant_type = array('accesstoken' => 'client_credential');
    private $curl_url = array('accesstoken' => 'https://api.weixin.qq.com/cgi-bin/token?', 'sendMenu' => 'https://api.weixin.qq.com/cgi-bin/menu/create?', 'upload' => 'http://file.api.weixin.qq.com/cgi-bin/media/upload?', 'getmedia' => 'https://api.weixin.qq.com/cgi-bin/media/get?');
    private $uploadtype = array(1 => 'image', 2 => 'voice', 3 => 'video', 4 => 'thumb');
    private $appid;
    private $secret;
    private $accesstoken;
    function __construct($appid, $secret, $accesstoken = NULL)
    {
        $this->appid = $appid;
        $this->secret = $secret;
        $this->accesstoken = $accesstoken;
    }

    public function GetMaterial($type, $mediaid)
    {
        if (!$this->accesstoken) {
            $resultjson = $this->GetAccessToken();
            $resultarr = json_decode($resultjson, true);
            $this->accesstoken = $resultarr['access_token'];
        }
        $url = $this->curl_url['getmedia'];
        $param = array();
        $param['access_token'] = $this->accesstoken;
        $param['media_id'] = $mediaid;
        $url = $this->GetUrl($url, $param);
        $resultcontent = $this->_requestUrl($url);
        $path = time() . rand(0, 999) . '.' . $type;
        //保存磁盘
        $imagepath = Storage::disk('wechat')->put($path, $resultcontent);
        if (!$imagepath) return $imagepath;
        return $path;
    }
    public function UploadMaterial($type, $filepath)
    {
        if (!$this->accesstoken) {
            $resultjson = $this->GetAccessToken();
            $resultarr = json_decode($resultjson, true);
            $this->accesstoken = $resultarr['access_token'];
        }
        $url = $this->curl_url['upload'];
        $param = array();
        $param['access_token'] = $this->accesstoken;
        $param['type'] = $this->uploadtype[$type];
        $url = $this->GetUrl($url, $param);
        $postfile = array('media' => '@' . $filepath);
        $result = $this->_requestUrl($url, $postfile, true);
        return $result;
    }
    private function GetAccessToken()
    {
        $url = $this->curl_url['accesstoken'];
        $param = array();
        $param['grant_type'] = $this->grant_type['accesstoken'];
        $param['appid'] = $this->appid;
        $param['secret'] = $this->secret;
        $url = $this->GetUrl($url, $param);
        $accesstoken = $this->_requestUrl($url);
        return $accesstoken;
    }
    private function _requestUrl($url, $param = null, $ispost = false)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }
    private function GetUrl($url, $param)
    {
        $params = http_build_query($param);
        $url = $url . $params;
        return $url;
    }
}

调用

 public function getJssdkMedia(Request $request)
    {
        $config = config('wechat.official_account.default');
        $GetWCApi =  new  GetWCApi($config['app_id'], $config['secret']);
        $getresult = $GetWCApi->GetMaterial('jpg',$request->mediaId);
        return $this->success(compact('getresult'));
    }
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!