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

在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"}
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 用MATLAB解决问题,需要代码
      • ¥45 关于C语言设计井字棋游戏问题的实现
      • ¥20 vuforia的Unity3D开发,模型附上特效后大量报错
      • ¥35 Python实现文本关键词提取
      • ¥20 如何使用c++语言完成实验仪器管理系统
      • ¥15 请问Unity使用Magica Cloth插件怎么调整散兵的帘子和振袖比较好?
      • ¥15 请问为什么原神模型导入Unity做动画默认姿势会站不直?
      • ¥15 ld: cannot open C:\Program: No such file or directory
      • ¥15 请问汏佬要怎么访问这种过滤了post和get的php内容?
      • ¥15 C语言调用python脚本出现Debug Assertion Failed错误