php使用curl调用java接口上传[multipart/form-data]图片!
接口文档
接口提供方是java,具体实现未知,一开始用h5直接提交到接口地址没毛病。后来改成先php上传到自己服务器再通过curl去post到接口地址。然后就出现问题了,下面是解决办法:
curl malformed
这个curl malformed报错原因是curl请求的url设置错误,(lll¬ω¬)
前端通过ajax每次上传一张图片,成功后返回地址展示前端
$.ajax({
url: "/app/score/uploadImg",
type: 'POST',
data: formData,
async: false,
cache: false,
dataType: 'json',
crossDomain: true,
contentType: false,
processData: false,
beforeSend: function (XMLHttpRequest) {
$("#loading").css("display", "block")
},
success: function (data) {
commonalter(data.msg);
if (data.code == 200) {
$(".z_file").append("<img class='imgList' onclick='imgClick(this)' src=" + data.data.data + ">");
}
},
complete: function (XMLHttpRequest, textStatus) {
$("#loading").css("display", "none");
},
error: function () {
}
});
后端最终代码:
/*
* 积分兑换上传图片(报单)
*/
public function uploadImg(){
set_time_limit(0);
ini_set('memory_limit','500M');
$upload_path = DS . 'uploads' . DS . 'orderlist'.DS;
$public_path = 'public' . $upload_path;
$type = $_FILES['fileImg']['type'];
$file_name = $_FILES['fileImg']['name'];
$tmp_name = $_FILES['fileImg']['tmp_name'];
$new_file = ROOT_PATH.$public_path.$file_name;
move_uploaded_file($tmp_name,$new_file);
$data = [
'fileImg' => new \CURLFile(realpath($new_file)),//'@'.realpath($tmp_name),
"filename" => $file_name
];
$result = $this->curl_request(config('jifen.url').'/api/order/uploadImg.remote', $data);
$result = json_decode($result,true);
if($result['code'] == 1){
$this->response($code=200,$msg='上传成功',$data = ['data'=>config('jifen.url').$result['fileName']]);
}else{
$this->response($code=400,$msg= $result['msg'],$data = ['tmp_name'=>realpath($tmp_name),'file_name'=>$file_name,'type'=>$type]);
}
}
private function curl_request($url,$post_data)
{
// 模拟提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
#curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
#curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
print curl_error($curl);
}
curl_close($curl); // 关键CURL会话
return $result; // 返回数据
}
使用如下代码报错php curl上传图片所需的多部分文件参数不存在
HTTP Status 400 - Required MultipartFile parameter 'fileImg' is not present
$post_data = [
'fileImg'=> '@'.ROOT_PATH .$public_path.$info->getSaveName(),
'filename'=>$info->getFileName(),
];
$post_data = [
'fileImg'=> new \CURLFile(ROOT_PATH .$public_path.$info->getSaveName()),
'filename'=>$info->getFileName(),
];
$file_info = $this->curl_request($url=config('jifen.url').'/api/order/uploadImg.remote',$post_data);
上传图片java服务端报错
HTTP Status 400 - Required MultipartFile parameter 'fileImg' is not present
type Status report
message Required MultipartFile parameter 'fileImg' is not present
description The request sent by the client was syntactically incorrect.
Apache Tomcat/7.0.86
上传图片小于1M的很快传成功,
$upload_path = DS . 'uploads' . DS . 'orderlist'.DS;
$public_path = 'public' . $upload_path;
$allow_size = 1048576;
$file = $this->request->file('fileImg');
$info = $file->rule('date')->validate(['size'=>3 * $allow_size])->move(ROOT_PATH.$public_path);
if($info){
if($info->getSize() >$allow_size){//压缩图片
$file_name = 'thumb_'.$info->getFilename();
$new_file = $info->getPath().DS.$file_name;
$image = Image::open($info->getPathname());
$image->thumb(750,1334,1)->save($new_file);
}else{//直接上传
$file_name = $info->getFilename();
$new_file = $info->getPathname();
}
$data = [
'fileImg' => new \CURLFile($new_file),//'@'.realpath($tmp_name),
"filename" => $file_name
];
$result = $this->curl_request(config('jifen.url').'/api/order/uploadImg.remote', $data);
$result = json_decode($result,true);
if($result['code'] == 1){
$this->response($code=200,$msg='上传成功',$data = ['data'=>config('jifen.url').$result['fileName']]);
}
}
$this->response($code=400,$msg= $file->getError(),$data = []);
网友评论