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

Multipart form file upload POST PHP cURL

我想使用 PHP 和 cURL 通过 http POST 发送文件。

除了使用 'application/json' 发布的文件之外,表单 POST 的基本字段工作正常。根据我的理解,这需要 multipart/form。

我得到的错误是 <b>注意:数组到字符串的转换</b> 在线
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

如果有人能提供帮助那就太好了!

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>

我看到的一个遗漏是您需要将 $cfile 对象添加到 $data 数组。这一点,再加上 Samir 的回答,应该让你们都准备好了。

您需要为要发布的数据构建查询字符串。使用 http_build_query

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

嗨,我已经找到问题了。

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

但我现在遇到的问题是一次 post 处理所有数据。创建笔记后,我正在 posting 文件,从而为我生成 posting 文件的笔记 ID。有人可以帮忙吗?我可以 post 一个新问题。

查看下面的更新代码:

//$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 从 Web 表单生成 PDF 文档然后自动发送而不是文件上传的解决方案。

这一切都变得非常混乱,而且 "contradictory" 很快! 因为 cUrl 非常灵活和强大,使用上下文中的微小差异会产生巨大影响,并可能导致 non-existent 天的错误追踪。

其次,您post想要的URL/Endpoint也可以拥有自己的"implementation"和期望。当仅使用 GET 请求并且 assumption/expectation 基于 PHP 超级简单 $_REQUEST

时,这种理解会变得复杂

在上面的例子中,简化:这是一个实时工作的例子,但是 post 字段 ($post) 已经从使用的 50 个减少到例如几个。这将 post 作为多部分表单数据。

这里重要的选项是使用 CURLOPT_POST 和 CURLOPT_POSTFIELDS 而不是 CURLOPT_CUSTOMREQUEST 的选项。这样做将需要您处理所需的 header,尤其是内容大小 header 等

// Post "field" array. Notice its 1 Dimensional. Else, this should rather accept JSON. simply decode array to json and proceed). Depends what server is expecting - Form data, Form + files, json or maybe just a RAW request body.
$post = array(  
            'RsmMaster1_TSM' => $tsm,
            '__EVENTTARGET' => '__Page',
            '__EVENTARGUMENT' => 'ExcelExport',
            '__VIEWSTATE'  => $viewstate,
            '__VIEWSTATEGENERATOR'  => $viewstategenerator,
            '__EVENTVALIDATION'  => $eventvalidation
 );

// Optional, Required in THIS case because, well gosh darn who knows, as the receiving server admin?
$headers = array(   
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Language: en-ZA,en-GB;q=0.8,en-US;q=0.5,en;q=0.3',
                'Referer: https://example.com/Main.aspx'  
            );

// cUrl it into the goal!
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://example.com/Main.aspx');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');                                                                   
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    
curl_setopt($ch, CURLOPT_HEADER, 0);
// curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); // Session maintain!
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FAILONERROR,    TRUE   );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE  );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE  );

$content = curl_exec($ch);
curl_close($ch);