微信素材管理接口 基于TP5

class Material extends Controller
{
    //微信素材相关url
    private $materialUrl = [

        //上传素材列表
        'upload' => 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%s&type=type',

        //计算总数
        'calculateTotal' => 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=',

        //获取列表
        'materialList' => 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=',
    ];

    //全局TOKEN
    private $globalToken = '';

    private $totalKey = 'WechatMaterialTotal';


    //获取素材列表
    public function getMaterial()
    {
        $page = $this -> request -> param('page', 1,'intval');

        //获取素材类型
        $type = $this -> request -> param('type', 'image');

        $requestData = [
            'type' => 'image',
            'offset' => (1 - $page) * 20,
            'count' => 20,
        ];

        echo self::httpsRequest($this->materialUrl['materialList'] . $this -> globalToken, json_encode($requestData));

    }


    public function uploadMaterial() {
        if($this->request->isPost()) {
            $file = $this -> request -> file('file');

            //tmp_name 文件的绝对路径
            //type     文件类型
            //name     文件名称
            $data = ['media'=> new \CURLFile($file->getInfo('tmp_name'), $file->getInfo('type'), $file->getInfo('name'))];

            echo self::httpsRequest($this->materialUrl['upload'], $data);
        }
    }



    private function httpsRequest($url, $data = null,$time_out=60,$out_level="s",$headers=array())
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        if($out_level=="s") {
            //超时以秒设置
            curl_setopt($curl, CURLOPT_TIMEOUT,$time_out);//设置超时时间
        } elseif ($out_level=="ms") {
            curl_setopt($curl, CURLOPT_TIMEOUT_MS,$time_out);  //超时毫秒,curl 7.16.2中被加入。从PHP 5.2.3起可使用
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if($headers) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//如果有header头 就发送header头信息
        }
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    public function httpGet($url, $header = '') {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);

        if(!empty($header)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值