通过cURL PHP将文件上传到API

时间:2018-04-18 10:20:08

标签: php api curl

过去几天我一直试图通过CURL将文件上传到远程API,运气不佳。代码在Windows上完美运行,我从API获得所需的返回输出。

我的问题是,一旦我转移到Linux环境并通过CURL上传文件,我会收到以下错误消息。

我尝试了什么: 我想也许权限和喜欢是我的问题的原因,因为它说它找不到文件,所以我决定稍微修改我的代码上传到服务器,将其移动到上传文件夹然后选择该文件发送通过curlfile但仍然没有运气。这是我得到的错误:

"errors": [
    {
      "developerMessage": "Error while manipulating file /home/USER/public_html/top/tmp/client17.jpg due to a File system / Amazon S3 issue /usr/share/tomcat7/.fineract/top/documents/clients/1/8q9sr/home/USER/public_html/top/tmp/client17.jpg (No such file or directory)",
      "defaultUserMessage": "Error while manipulating file /home/USER/public_html/top/tmp/client17.jpg due to a File system / Amazon S3 issue /usr/share/tomcat7/.fineract/top/documents/clients/1/8q9sr/home/USER/public_html/top/tmp/client17.jpg (No such file or directory)",
      "userMessageGlobalisationCode": "error.msg.document.save",
      "parameterName": "id",
      "value": null,
      "args": [
        {
          "value": "/home/USER/public_html/top/tmp/client17.jpg"
        },
        {
          "value": "/usr/share/tomcat7/.fineract/top/documents/clients/1/8q9sr/home/USER/public_html/top/tmp/client17.jpg (No such file or directory)"
        }
      ]
    }
  ]

这里是我正在使用的代码:

// UPLOAD NEW FILES
if(ISSET($_FILES))
{

    $uploads = array('guarantor_id_card', 'guarantor_payslip_1', 'guarantor_payslip_2', 'guarantor_payslip_3', 'guarantor_bank_1', 'guarantor_bank_2','guarantor_bank_3' );
    $postFields = array();

    // for creating documents

    foreach($uploads as $k => $v)
    {

        // GET  UPLOAD NAME FROM ARRAY 
        $upload_name = $v;

        if(empty($_FILES[$upload_name]['error']))
        {

            $uploaddir = getcwd() . "/tmp/";
            $uploadfile = $uploaddir . basename($_FILES[$upload_name]['name']);

            if (move_uploaded_file($_FILES[$upload_name]['tmp_name'], $uploadfile)) {
              echo "File is valid, and was successfully uploaded.\n";
            } else {
               echo "Upload failed";
            }           


            //files
            $postFields['file'] = new CURLFile($uploadfile);

            //metaData USING COUNTER AND UPLOAD NAME DYNAMIC
            $postFields['fileName'] = $_FILES[$upload_name]['name'];
            $postFields['type'] = $_FILES[$upload_name]['type'];
            $postFields['name'] = 'Supporting Docs';
            $postFields['description'] = $_FILES[$upload_name]['name'];

            // initialise the curl request
            $request = curl_init("https://APIURL_HERE");

            // send a file
            curl_setopt($request, CURLOPT_POST, true);
            curl_setopt($request, CURLOPT_USERPWD, $user . ":" . $password);
            curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
            curl_setopt($request, CURLOPT_PORT, 8443);
            curl_setopt($request, CURLOPT_SAFE_UPLOAD, TRUE);
            curl_setopt($request, CURLOPT_POSTFIELDS, $postFields);

            // output the response
            curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($request, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
            $send_request = curl_exec($request);

            // close the session
            curl_close($request);
            echo $send_request;

        } 

    }

    echo '<pre>';
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    print_r(getcwd());
    echo '<br>';
    print_r($uploadfile);
    print "</pre>";

}

我没有想法,如果有人有这方面的经验,请帮忙!

感谢。

0 个答案:

没有答案