微信第三方开发,上传永久素材

include_once "wxstore/wxClassLib.php";
        $authorizer_access_token = get_token();
        $wxClassLib = new wxClassLib($authorizer_access_token);
        if($size < 2048000 )
        {
            $updata['filename'] = $file_name;
            $updata['content-type'] = $_FILES['pic']['type'];
            $updata['filelength'] = $size;
            $real_file = ROOT_PATH . $file;


            $upres = $wxClassLib->up_media_towx('image', $real_file, $updata);
            if($upres !="")
            {
               
            }

        }

//刷新token

function get_token()
{
    $sql = "select “...;
    $res = $GLOBALS['db']->getRow($sql);
    $token = "";
    if ($res['expire_time'] < time())
    {
        include_once "wxstore/wxAuthLib.php";
        $wxauth = new wxAuthLib();
        $get_token = $wxauth->get_authorizer_refresh_token($res['authorizer_refresh_token'], $res['authorizer_appid']);
        if($get_token != "")
        {
            $token = $get_token['authorizer_access_token'];
            $expire_time = time()+7000;
            $GLOBALS['db']->query("UPDATE "...);
        }else
        {
            $token = "";
        }
    }else
    {
        $token = $res['authorizer_access_token'];
    }
    return $token;
}


       /*
*获取(刷新)授权公众号的接口调用凭据(令牌)
*/
public function get_authorizer_refresh_token($authorizer_refresh_token, $authorizer_appid)
{
$component_access_token = $this->getComponentAccessToken();
      $url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=".$component_access_token; //文档中,url中api前和com后多了空格
      $curlPost['component_appid'] = $this->appId;
      $curlPost['authorizer_appid'] = $authorizer_appid;
      $curlPost['authorizer_refresh_token'] = $authorizer_refresh_token;


      $curlPost = json_encode($curlPost);
      $res = json_decode($this->helper->httpPost($curlPost, $url), true);
      $authorizer_access_token = $res ['authorizer_access_token'];
     if($authorizer_access_token != "")
     {
      return $res;
     }else
     {
      return "";
     }
}


       /*
*新增永久媒体文件
*/
public function up_media_towx($type, $img_path, $data)
{
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->access_token."&type=".$type;
/*$file_info=array(
   'filename'=>'/images/1.png',  //国片相对于网站根目录的路径
   'content-type'=>'image/png',  //文件类型
   'filelength'=>'11011'         //图文大小
);*/
        if($data['filename'] != "")
        {
        $curlPost['media'] =  '@' . $img_path;
        $curlPost['form-data'] = $data;
        $res = json_decode($this->helper->httpPost($curlPost, $url), true);
        if(isset($res['media_id']))
        {
        return $res;
        }else
        {
        return "";
        }
        }else
        {
        return "";
        } 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值