发布时间:2022-06-20 11:24:09 点击数:450
微信网页开发中一般要用到多图上传,这时涉及微信jssdk图片上传,上传的需要需要用PHP从素材库中下载,PHP微信开发中,如何下载保存微信素材库的图片
以下是参考代码:
public function downloadWeixinFile($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0); //只取body头
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$package = curl_exec($ch);
$httpinfo = curl_getinfo($ch);
curl_close($ch);
$imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
return $imageAll;
}
public function saveWeixinFile($filename, $filecontent){
$local_file = fopen($filename, 'w');
if (false !== $local_file){
if (false !== fwrite($local_file, $filecontent)) {
fclose($local_file);
return $filename;
}
}
}
调用方法:
$mediaId = I('put.mediaId');
$conf = C('weiXin.mp');
$weApp = new \Org\Util\Weapp($conf);
$url = $weApp->getMediaUrl($mediaId);
$arr = $this->downloadWeixinFile($url);
$file = time();
$localFile = $this->saveWeixinFile("Uploads/media/{$file}.jpg",$arr['body']);