微信公众平台开发—上传永久素材(图片)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信公众平台开发—上传永久素材(图片)相关的知识,希望对你有一定的参考价值。
这段代码有错吗?为什么执行后返回的是media data missing,请大牛不吝赐教!或者给一份能正常使用的上传永久素材的php代码。万分感谢!
class test
public function upload()
include 'getAccessToken.php';
$obj = new getAccessToken();
$Access_Token = $obj->getStr();
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$Access_Token&type=image";
$file_info = array(
'filename' => "F:/svn/wx/baby.jpg",
'content-type'=>"image/jpeg",
'filelength'=>"78710"
);
$data= array("media"=>"@F:/svn/wx/baby.jpg",'form-data'=>$file_info);
$rs = $this->http_request($url,$data);
var_dump($rs);
private function http_request($url, $data = null)
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt ( $curl, CURLOPT_CONNECTTIMEOUT, "5");
if (! empty($data))
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
$test = new test();
$test->upload();
tp5上传图片添加永久素材到微信公众号
$file = request()->file(‘image‘);
if(!$file){
$res[‘status‘] = false;
$res[‘msg‘] = ‘必须上传文件‘;
return $res;
}
// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->validate([‘size‘=>351600,‘ext‘=>‘jpg,png,gif‘])->move(ROOT_PATH . ‘public‘ . DS . ‘uploads‘);
if($info){
// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
$photo = $info->getSaveName();
}else{
$msg = $file->getError();
$res[‘status‘] = false;
$res[‘msg‘] = $msg;
return $res;
}
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type=image";
$ch1 = curl_init();
$timeout = 5;
$real_path = "{$_SERVER[‘DOCUMENT_ROOT‘]}uploads/{$photo}";
$real_path=str_replace("\\", "/", $real_path);
$path = new \CURLFile($real_path);
$data = array(‘media‘=>$path);
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch1);
print_r($result);
很多是这样传参数的,$data
[
‘media‘
] =
"@/www/file/test.png"
;
$data
[
‘form-data ‘
] =
array
(
‘filename‘
=>
‘test.png‘
);
$data
[
‘media‘
] =
"@/www/file/test.png;type=image;filename=test.png;filelength=1024;content-type=image/png"
;
new \CURLFile
经过测试可用
参数可以只传文件的路径 $data = array(‘media‘=>$path);
希望对大家开发有一定的帮助
以上是关于微信公众平台开发—上传永久素材(图片)的主要内容,如果未能解决你的问题,请参考以下文章
Java微信公众平台开发之素材管理(Spring Boot 2.X)
请问微信公众平台上传图片的数量是多少?或者上传上限是多少KB?谢谢!