微信新增临时素材-图片

微信新增临时素材

这里写图片描述
这里写图片描述
这里写图片描述

获取access_token—getaccesstoken.php

<?php  
    //7200s内获取access_token
    function getAccessToken(){
        if(file_exists('access_token.txt')){
            $time=filemtime('access_token.txt');
            $nowtime=time();
            if($nowtime-$time>7200){
                unlink('access_token.txt');
                $res= wechatAccessToken();
            }else{
                $str=file_get_contents('access_token.txt');
                $arr=json_decode($str,true);
                $res= $arr['access_token'];
            }
        }else{
            $res= wechatAccessToken();
        }
        return $res;
    }
    //请求获取access_token接口
    function wechatAccessToken(){
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=***&secret=***';
        $str=file_get_contents($url);
        $arr=json_decode($str,true);
        $access_token=$arr['access_token'];
        file_put_contents('access_token.txt', $access_token);
        return $access_token;
    }
    // echo getAccessToken();
?>

模拟表单上传图片素材—form.php

<?php  
    include('getaccesstoken.php');
    $access_token=getAccessToken();
    $url='https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$access_token.'&type=image';
    $data=array(
        'media'=>new CURLFile(realpath('./banner_02.jpg'))
    );
    $res=postCurl($url,$data);
    print_r($res);

//{"type":"image","media_id":"JfY0syKKmrovPoOrn7Q5ZJcgs_eC9uMLoALkPwmwJiCxacod7JR4QNMfBq0g0aKB","created_at":1501067593}

    function postCurl($url,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        curl_close($ch);
        return $tmpInfo;
    }
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值