$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 "";
}
}