且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多部分表单文件上传POST PHP cURL

更新时间:2023-02-21 21:14:46

我已经解决了问题.

我的参数在 api 端点上设置不正确.需要设置一个note_id(c_id)

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

但我现在遇到的问题是一次发布所有数据.我在创建注释后发布文件,从而为我生成用于发布文件的注释 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.

查看下面的更新代码:

//$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>

如果有人感兴趣,这是我使用 fpdf 从网络表单生成 PDF 文档然后自动发送而不是文件上传的解决方案.FPDF文件--->通过 CURL 自动发送而不是文件上传

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