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

在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条回答 默认 最新

  • 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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵