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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100