Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将请求php提交给spring_Php_Spring_Curl_Post - Fatal编程技术网

将请求php提交给spring

将请求php提交给spring,php,spring,curl,post,Php,Spring,Curl,Post,我必须使用PHP和cURL将文件数组发送到spring服务器 这是弹簧控制器: @RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST) public HttpStatus uploadFiles(@RequestParam("files") MultipartFile[] inputFiles, @RequestParam("assignment

我必须使用PHP和cURL将文件数组发送到spring服务器

这是弹簧控制器:

@RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST) 
public HttpStatus uploadFiles(@RequestParam("files") MultipartFile[] inputFiles, 
                              @RequestParam("assignmentID") String assignmentID) throws IOException { ... }
PHP cURL:

<?php 
$filenames = array(pathfile1, pathfile2);

$postparameters = array(
  'files' =>  array(
                    new CURLFile($filenames[0], "text/plain", pathinfo($filenames[0], PATHINFO_BASENAME)),
                    new CURLFile($filenames[1], "text/plain", pathinfo($filenames[1], PATHINFO_BASENAME))
                  ),
  'assignmentID' => "0008"         
  );

print_r($postparameters);


//init curl
$url = "http://localhost:8090/upload/teacher";


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => $postparameters,
  CURLOPT_HTTPHEADER => array(
    "Content-Type: multipart/form-data"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
它也适用于邮递员:


有人能告诉我为什么在我运行php请求时后端没有收到文件吗?

如果用
@CrossOrigin
注释spring控制器方法会发生什么

@CrossOrigin
@RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST) 
public HttpStatus uploadFiles(@RequestParam("files") MultipartFile[] inputFiles, 
                              @RequestParam("assignmentID") String assignmentID) throws IOException { ... }

如果使用
@CrossOrigin
注释spring控制器方法,会发生什么情况

@CrossOrigin
@RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST) 
public HttpStatus uploadFiles(@RequestParam("files") MultipartFile[] inputFiles, 
                              @RequestParam("assignmentID") String assignmentID) throws IOException { ... }
您是否添加了:@CrossOrigin(origins=“http://yourphp-host”)您是否添加了:@CrossOrigin(origins=“http://yourphp-host)