如何在PHP中通过调用RESTAPI上传图像?
我在拉维尔做一个项目。表单提交后,我想将上传的文件移动到RESTAPI文件夹。 为此,我在控制器方法中调用了curl函数 controller.php如何在PHP中通过调用RESTAPI上传图像?,php,rest,laravel,curl,Php,Rest,Laravel,Curl,我在拉维尔做一个项目。表单提交后,我想将上传的文件移动到RESTAPI文件夹。 为此,我在控制器方法中调用了curl函数 controller.php { if($request->hasFile('filefield')){ $file = $request->file('filefield'); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_fi
{
if($request->hasFile('filefield')){
$file = $request->file('filefield');
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $file);
finfo_close($finfo);
$filename = $file->getClientOriginalName();
$method = 'POST';
$url = 'engagev1be.taarago.in/superadmin';
$data = array('file' => new \CurlFile($file,$mimeType,$filename), 'oldpath' =>$oldpath,'id'=>$id);
$result = $this->CallAPI($method,$url,$data);
}
}
而调用API函数就像
public function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method)
{
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
静态api myRoute.php是
Route::post('superadmin', 'SuperadminController@uploadimg');
public function uploadimg(Request $request){
$file = $request->file;
$oldpath = $request->oldpath;
$id = $request->id;
$editProvider = Provider::findOrFail($id);
if($file){
$filename = $file->getClientOriginalName();
$filename = str_replace('%', ' ', $filename);
$newpath = 'images/provider_images/'.$id;
if (!file_exists($newpath)) {
File::makeDirectory($newpath, 0777, true, true);
$newpath = 'images/provider_images/'.$id;
}
$success = move_uploaded_file($oldpath,$newpath.'/'.$filename);
$editProvider->profile_pic = $newpath.'/'.$filename;
$editProvider->update();
return $success ;
}
}
和SuperadminController@uploadimg是
Route::post('superadmin', 'SuperadminController@uploadimg');
public function uploadimg(Request $request){
$file = $request->file;
$oldpath = $request->oldpath;
$id = $request->id;
$editProvider = Provider::findOrFail($id);
if($file){
$filename = $file->getClientOriginalName();
$filename = str_replace('%', ' ', $filename);
$newpath = 'images/provider_images/'.$id;
if (!file_exists($newpath)) {
File::makeDirectory($newpath, 0777, true, true);
$newpath = 'images/provider_images/'.$id;
}
$success = move_uploaded_file($oldpath,$newpath.'/'.$filename);
$editProvider->profile_pic = $newpath.'/'.$filename;
$editProvider->update();
return $success ;
}
}
在本例中,$success返回none,因为文件未上载。
我在做什么,当我做的时候
dd($request->all());
那么结果呢,
array:3 [▼
"oldpath" => "/tmp/phpmyoWT4"
"id" => "61"
"file" => UploadedFile {#30 ▼
-test: false
-originalName: "images.png"
-mimeType: "image/png"
-size: 5126
-error: 0
}
]
这意味着,文件已成功返回到RESTAPI,但未上载