duansaoguan7955
2018-05-11 08:31
浏览 110
已采纳

在php中使用curl添加表单数据

I'm trying to convert this curl request to PHP:

curl -X POST \
    --form fileItems[0].fileToUpload=@"/path/to/file1.txt"  \
    --form fileItems[0].path="/path1/path2/"    \
    --form fileItems[0].replacing=true  \
    --form fileItems[1].fileToUpload=@"/path/to/file2.txt"  \
    --form fileItems[1].path="/path1/path3/"    \
    --form fileItems[1].replacing=true  \
    http://example.com/files

Here is my code:

$ch=curl_init("http://example.com/files");

$cfile= new CURLFile($_FILES['uploaded_files']['tmp_name'],
                     $_FILES['uploaded_files']['type'],
                     $_FILES['uploaded_files']['name']);

$data=array("fileItems" => array("fileToUpload" => $cfile,
                                 "path" => "/",
                                 "replacing" => true
                                ));

After uploading a file and trying to send it to the server I get this message:

Notice: Array to string conversion in C:\...\uploadToBacktory.php on line 48

line 48:

curl_setopt($ch,CURLOPT_POSTFIELDS, $data);

API server error:

{"timestamp":1526026978184,"status":400,"error":"Bad Request","exception":"org.springframework.validation.BindException","errors":[{"codes":["typeMismatch.fileUploadRequest.fileItems","typeMismatch.fileItems","typeMismatch.java.util.List","typeMismatch"],"arguments":[{"codes":["fileUploadRequest.fileItems","fileItems"],"arguments":null,"defaultMessage":"fileItems","code":"fileItems"}],"defaultMessage":"Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'fileItems'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'ir.pegahtech.backtory.cdn_api.models.controllers.files.FileUploadItem' for property 'fileItems[0]': no matching editors or conversion strategy found","objectName":"fileUploadRequest","field":"fileItems","rejectedValue":"Array","bindingFailure":true,"code":"typeMismatch"}],"message":"Validation failed for object='fileUploadRequest'. Error count: 1","path":"/files"}

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将此curl请求转换为PHP:

 <  code> curl -X POST \
 --form fileItems [0] .fileToUpload = @“/ path / to / file1.txt”\
 --form fileItems [0] .path =“/ path1 / path2 /”  \
 --form fileItems [0] .replacing = true \
 --form fileItems [1] .fileToUpload = @“/ path / to / file2.txt”\
 --form fileItems [1] .path  =“/ path1 / path3 /”\
 --form fileItems [1] .replacing = true \
 http://example.com/files
nn

这是我的代码:

  $ ch = curl_init(“http://example.com/files”); 
 
 $ cfile = new CURLFile($ _ FILES  ['uploaded_files'] ['tmp_name'],
 $ _FILES ['uploaded_files'] ['type'],
 $ _FILES ['uploaded_files'] ['name']); 
 
 $ data = array  (“fileItems”=&gt; array(“fileToUpload”=&gt; $ cfile,
“path”=&gt;“/”,
“replacement”=&gt; true 
)); 
    
 
 

上传后 一个文件并尝试将其发送到服务器我收到此消息:

 注意:第48行的C:\ ... \ uploadToBacktory.php中的数组到字符串转换 
   
 
 

第48行:

  curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data); 
   
 
 

API服务器错误:

  {“timestamp”:1526026978184,“status”:400,“error”:“错误请求 ”, “异常”: “org.springframework.validation.BindException”, “错误”:[{ “代码”:[ “typeMismatch.fileUploadRequest.fileItems”, “typeMismatch.fileItems”, “typeMismatch.java.util.List”  “typeMismatch”], “参数”:[{ “代码”:[ “fileUploadRequest.fileItems”, “fileItems”], “参数”:NULL, “defaultMessage”: “fileItems”, “代码”: “fileItems”}  ],“defaultMessage”:“无法将类型'java.lang.String'的属性值转换为属性'fileItems'所需的类型'java.util.List'; 嵌套异常是java.lang.IllegalStateException:无法将类型'java.lang.String'的值转换为属性'fileItems [0]'所需的类型'ir.pegahtech.backtory.cdn_api.models.controllers.files.FileUploadItem': 找不到匹配的编辑器或转换策略“,”objectName“:”fileUploadRequest“,”field“:”fileItems“,”rejectedValue“:”Array“,”bindingFailure“:true,”code“:”typeMismatch“}],” 消息“:”object ='fileUploadRequest'的验证失败。 错误计数:1“,”path“:”/ files“} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpppic5186 2018-05-11 10:06
    已采纳

    problem solved by changing $data variable to this:

    $data=['fileItems[0].path' => '/', 'fileItems[0].replacing' => true, 'fileItems[0].fileToUpload' => $cfile];
    
    点赞 打赏 评论

相关推荐 更多相似问题