Multipart form file upload POST PHP cURL

北城余情 提交于 2019-12-13 14:26:24

问题


I am wanting to send a file via http POST using PHP and cURL.

The form POST was working ok with basic fields besides the file being posted with 'application/json'. This needs to be multipart/form from what I understand.

Error I am getting is Notice: Array to string conversion on line
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

If anyone can help that would be great!

PHP

$orgID = (is_numeric($_POST['orgID']) ? (int)$_POST['orgID'] : 0);
$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);

if(isset($_FILES['file']['tmp_name'])){

    $ch = curl_init();
    $cfile = new CURLFILE($_FILES['file']['tmp_name'], $_FILES['file']['type'], $_FILES['file']['name']);
    $data = array();                

    $data["TITLE"] = "$noteTitle";
    $data["BODY"] = "$noteBody";
    $data["LINK_SUBJECT_ID"] = "$orgID";
    $data["LINK_SUBJECT_TYPE"] = "Organisation";        
    $data['FILE_ATTACHMENTS']['FILE_NAME'] = $_FILES['file']['name'];
    $data['FILE_ATTACHMENTS']['CONTENT_TYPE'] = $_FILES['file']['type'];
    $data['FILE_ATTACHMENTS']['URL'] = $_FILES['file']['tmp_name'];

    $localFile = $_FILES['file']['tmp_name'];
    $fp = fopen($localFile, 'r');       

    $headers = array(
        "authorization: Basic xxx",
        "cache-control: no-cache",
        "content-type: multipart/form-data",
        "postman-token: xxx"
    );

    curl_setopt($ch, CURLOPT_URL, "https://api.insight.ly/v2.1/Notes");
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_NOPROGRESS,false); 
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);

    if ($response === true) {
        $msg = 'File uploaded successfully.';    
    }
    else {
        $msg = curl_error($ch);         
    }

    curl_close ($ch);

    $return = array('msg' => $msg);

    echo json_encode($return);
}

HTML

<form method="POST" action="formSend.php" enctype="multipart/form-data">
    <input type="text" value="" name="orgID">
    <input type="text" value="" name="noteTitle">
    <input type="text" value="" name="noteBody">  
    <input name="file" type="file" id="file"/>
    <input type="submit" value="Submit" name="btnUpload"/>
</form>

回答1:


Hi I have figured out the problem.

My params were not set correct on the api endpoint. Need to set a note_id(c_id)

But issue I am having now is posting all data at once. I am posting file after the note has been created thus generating the note id for me for posting file. Can anyone help with that? I can post a new question.

See updated code below:

//$orgID = (is_numeric($_POST['orgID']) ? (int)$_POST['orgID'] : 0);
//$noteTitle = (isset($_POST['noteTitle']) ? $_POST['noteTitle'] : null);
//$noteBody = (isset($_POST['noteBody']) ? $_POST['noteBody'] : null);

$noteID = (isset($_POST['noteID']) ? $_POST['noteID'] : null);

$localFile = $_FILES['file']['tmp_name'];
$fp = fopen($localFile, 'r');

$curl = curl_init();

$cfile = new CURLFILE($_FILES['file']['tmp_name'], $_FILES['file']['type'], $_FILES['file']['name']);
$data = array();                
//$data["TITLE"] = "$noteTitle";
//$data["BODY"] = "$noteBody";
//$data["LINK_SUBJECT_ID"] = "$orgID";
//$data["LINK_SUBJECT_TYPE"] = "Organisation";        
$data['FILE_ATTACHMENTS'] = $cfile;

curl_setopt_array($curl, array(
  CURLOPT_UPLOAD => 1,
  CURLOPT_INFILE => $fp,
  CURLOPT_NOPROGRESS => false, 
  CURLOPT_BUFFERSIZE => 128,
  CURLOPT_INFILESIZE => filesize($localFile),
  CURLOPT_URL => "https://api.insight.ly/v2.1/Notes/?c_id=" . $noteID . "&filename=" . $_FILES['file']['name'],
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $data,      
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic xxx",
    "cache-control: no-cache",
    "content-type: multipart/form-data",
    "postman-token: xxx"
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

HTML

<form method="POST" action="formSend.php" enctype="multipart/form-data">
    //<input type="text" value="" name="orgID">
    //<input type="text" value="" name="noteTitle">
    //<input type="text" value="" name="noteBody"> 
    <input type="text" value="" name="noteID">
    <input name="file" type="file" id="file"/>
    <input type="submit" value="Submit" name="btnUpload"/>
</form>

If anyone is interested this is my solution for using fpdf to generate a PDF document from the web form then auto send, instead of the file upload. FPDF file ---> send via CURL automatically NOT with file upload




回答2:


You need to build query string for data to be posted. Use http_build_query

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));



回答3:


One omission I see is that you need to add your $cfile object to the $data array. This, coupled with the answer by Samir, should get you all squared away.



来源:https://stackoverflow.com/questions/34604602/multipart-form-file-upload-post-php-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!