场景:
前端上传base64格式的图片到服务器,接受图片信息并上传至七牛云报错
2.代码
/*
* base64 数据流上传
* */
Public function liuUpload($base_str,$suffix = ''){
header('Access-Control-Allow-Origin:*');
header("Content-type: image/jpg");
$base64 = explode(',', $base_str); // 头部的代码 data:image/jpeg;base64, 需要去掉
if($suffix){
$str = $base64[1];
/* $imgs = imgtobase64($str);
$imgarray = explode(',', $imgs);
$str = $imgarray[1];*/
}else{
$str = $base64[1];
}
//获取七牛配置
$qn_accesskey=config("Qiniu.accessKey");
$qn_secretkey=config("Qiniu.secretKey");
$qn_bucket=config("Qiniu.bucket");
$qn_domain=config("Qiniu.DOMAIN");
$auth = new Auth($qn_accesskey, $qn_secretkey);
//获取上传所需的token
$upToken = $auth->uploadToken($qn_bucket, null, 3600);
return $this->requestByCurl('http://up-z2.qiniup.com/putb64/-1/mimeType/jpeg',$str,$upToken);
}
public function requestByCurl($remote_server,$post_string,$upToken) {
$headers = array();
$headers[] = 'Content-Type:image/png';
$headers[] = 'Authorization:UpToken '.$upToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$remote_server);
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
3.解决方法
http://up-z2.qiniup.com/putb64/-1/mimeType/jpeg 改为 http://up-z2.qiniup.com/putb64/-1