微信公众平台上传永久素材
微信上传永久素材接口:http请求方式: POST,https协议https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN
上传微信永久素材的方式有两种,页面中的表单请求和使用curl模拟post请求,本文针对curl上传永久素材记录
表单上传文件至服务器上,接着请求访问接口的方法进行上传
public function upload_material($filename){
$proelling = new PropellingController();
$proelling->uploa_material_image($filename);
}
获取图片路径
public function uploa_material_image($filename){
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=access_token&type=image";
//访问微信上传永久素材接口
$filepath = dirname(dirname(dirname(dirname(__FILE__)))).$filename;
//$filepath的路径是针对于服务器上的图片绝对路径
$this->curl_box($url,$filepath);
}
使用curl上传
public function curl_box($url,$filepath){
$ch = curl_init();
$data = array('media' => new \CURLFile($filepath)); //使用curlfile上传图片文件
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$curl = curl_exec($ch);
//$error = curl_error($ch);//若有$curl=false,开启error查看错误信息
curl_close($ch);
return $curl;
}
来源:CSDN
作者:猫小疯
链接:https://blog.csdn.net/qq_45372060/article/details/94986138