微信新增临时素材-图片

微信新增临时素材

微信新增临时素材-图片_第1张图片
微信新增临时素材-图片_第2张图片
微信新增临时素材-图片_第3张图片

获取access_token—getaccesstoken.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

  
    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;
    }
?>

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