微信公众号通过接口上传图文素材

// 新增其他类型永久素材
public function wxAddMaterial($token,$filename='',$type='') {
    // 设置请求参数
    $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token['data']."&type=".$type;
    $filename = str_replace('http://localhost:8080/', '', $filename);
    $filePath = str_replace('\\', '/', 'D:/phpstudy_pro/WWW/files/'.$filename);
    $params = array();
    if ($type == 'video') {
        // 上传视频素材时需要POST提交description表单
        $params['description'] = urldecode(json_encode(array(
            'title' => 'default_title'.time(),
            'introduction' => 'default_message_video'
        )));
    }
    $params['media'] = new \CURLFile($filePath);
    // 发送请求
    $res = $this->https_request( $url ,'post', 'json', $params);
    return $res;
}


// 上传图文消息内的图片获取URL
public function wxUploadimg($token, $filename) {
    // 设置请求参数
    $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$token['data'];
    // $filename = str_replace('http://localhost:8080/', '', $filename);
    // $filePath = str_replace('\\', '/', 'D:/phpstudy_pro/WWW/files/'.$filename);
    $path = new \CURLFile($filePath);
    $data=array("media"=>$path);
    $res=$this->https_request( $url ,'post', 'json', $data);
    return $res;
}


private function https_request($url,$type="get",$res="json",$data = '') {
    //1.初始化curl
    $curl = curl_init();
    //2.设置curl的参数
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($type == "post"){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    //3.采集
    $output = curl_exec($curl);
    //4.关闭
    curl_close($curl);
    if ($res == 'json') {
        return json_decode($output,true);
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值