通过cURL PHP将上传的文件发送到API

时间:2019-03-13 18:16:56

标签: php api file curl

我正在尝试使用PHP通过cURL发送上传的文件。

   <?php
if (!isset($_POST['idcampagnestatus'])) {
    header("Location: exampleblablavacatures/?vacature=0&errors=1");
    die();
}

$file_path = 'cv/' . time() . '-' . $_FILES['docsbestand']['name'];

file_put_contents($file_path, file_get_contents($_FILES['docsbestand']['tmp_name']));

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "exampleapiurl",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"docsbestand\"; filename=\"" . 'exampleblablawp-content/plugins/hoezorgjij-api/' . $file_path . "\"\r\nContent-Type: "  . $_FILES['docsbestand']['type'] . "\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

unlink($file_path);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
    if (json_decode($response)->FOUT) {
        header("Location: exampleblablavacatures/?vacature=0&errors=1");
        die();
    } else {
        header("Location: exampleblablavacatures/?vacature=0&errors=0");
        die();
    }
}

我尝试了其他方法,但是经常得到一个空文件。我的想法是,我将文件保存到Web服务器,然后将其发送到API,API应该对其进行处理并将其放在自己的Web服务器上。通过API发送了更多详细信息,这些都可以正常工作。我想念什么?预先谢谢你。

0 个答案:

没有答案