微信卡券上传logo素材 php

//获取token

    public function getToken()
    {
        $appid = $this->config->item('appid');
        $secret = $this->config->item('secret');
        $url = "https://api.weixin.qq.com/cgi-bin/token";
        $data = "grant_type=client_credential"."&appid=".$appid."&secret=".$secret;
        $result = $this->curl_post($url,$data);
        return $result;

    }

$this->token = $this->getToken()['access_token'];  //随便一写 知道意思就行


//curl post请求

    public function curl_post($url,$data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );  // 上传
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($result, true);
        return $data;

    }

//上传卡券logo

public function upLogo($url,$token)
    {

        $uri = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$token}";

        //根据php版本CURLFile 选择上传方式

        if (class_exists('\CURLFile')) {
            $field = array('buffer' => new \CURLFile(realpath($url)));
        } else {
            $field = array('buffer' => '@' . realpath($url));
        }
        return $this->curl_post($uri,$field);

    }

    public function upload()
    {
        $path = './uploads/';
        $path .= date('Y-m-d', time());
        if (!is_dir($path)) {
            mkdir($path);
        }
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '10240';
        $config['max_width'] = '10240';
        $config['max_height'] = '76800';
        $config['file_name'] = time();
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('img')) {
            $error = array('error' => $this->upload->display_errors());
            $result['code'] = 400;
            $result['data'] = $error;
            $this->echoJson($result);
        } else {
            $data = $this->upload->data();
            $imgUrl = strstr($data['full_path'], 'uploads');
            $image = $this->upLogo($imgUrl, $this->token);     //调用上传
            if (isset($image['url'])) {
                $this->imgUrl = $image['url'];
                $result['code'] = 200;
                $result['data'] = '上传成功';
                $this->echoJson($result);
            } else {
                $image['code'] = 500;
                $this->echoJson($image);
            }
        }
    }





你可能感兴趣的:(微信)