tp5上傳圖片添加永久素材到微信公眾號
阿新 • • 發佈:2017-10-27
ken 傳參數 connect url lena www num 框架 替換
$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‘
);
經過測試不可用;
還有這種@上傳文件curl只支持php5,6以下的版本
$data
[
‘media‘
] =
"@/www/file/test.png;type=image;filename=test.png;filelength=1024;content-type=image/png"
;
對於php>5.6的需要將@替換成
new \CURLFile
經過測試可用
參數可以只傳文件的路徑 $data = array(‘media‘=>$path);
希望對大家開發有一定的幫助
tp5上傳圖片添加永久素材到微信公眾號