PHP POST API未收到文件

时间:2019-05-19 02:16:52

标签: php api curl post

我们的团队正在开发Web应用程序,以使用PHP远程访问3D打印机。我们尝试使用multipart / form-data实现POST print_job部分,但是它不起作用,这表明没有收到文件。该API将检查ID和密钥。这是代码。任何帮助表示赞赏!

它在Apache 2.4.39,PHP 7.3.5,XAMPP控制面板3.2.2上运行。

详细信息是:

<?php
function callAPI($method, $url, $data){
  $curl = curl_init();

  switch ($method){
     case "POST":
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data)
           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
     case "PUT":
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
        if ($data)
           curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                               
        break;
     default:
        if ($data)
           $url = sprintf("%s?%s", $url, http_build_query($data));
  }
  $username = "";
  $password = "";
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
  curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password); 
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($curl, CURLOPT_TIMEOUT, 90);
  curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);


  // EXECUTE:
  $result = curl_exec($curl);
  if(!$result){die("Connection Failure");}
  curl_close($curl);
  return $result;
}
?>



<?php
include('api.php');

$_SESSION['ip'] = "";
$_SESSION['url'] = "http://".$_SESSION['ip']."/api/v1";

$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file); 
$filedata = $_FILES["file"]["tmp_name"];
echo $target_file;
$data_array =  array(
  "jobname" => "file",
  "file" => "new \CURLFile(realpath($filedata))"
);
$_SESSION['size'] = $_FILES['file']['size'];
//$make_call = callAPI('POST', $_SESSION['url']."/print_job", $data_array);
$response = callAPI('POST', $_SESSION['url']."/print_job", $data_array);
$_SESSION['print'] = $response;
header('location: ../index.php');
?>

Ps:如果我想将从前端获取的文件上传到远程API,则可能必须将其存储在本地。然后,我尝试了以下代码。可以。

file_put_contents("E:/xyz/test.gcode",file_get_contents("../uploads/".$_FILES["file"]["name"]));
$filedata='E:/xyz/test.gcode';
if(!is_readable(realpath($filedata))){throw new \RuntimeException("upload file not readable!");}

$data_array =  array(
  'jobname' => 'file',
  'file' => new \CURLFile($filedata)
);

1 个答案:

答案 0 :(得分:0)

首先,请不要手动设置Content-Type:multipart/form-data标头,因为如果这样做,将会破坏边界。完整的标头看起来像:

Content-Type: multipart/form-data; boundary=------------------------777d48028c332f50

因此删除此内容:

$headers = array("Content-Type:multipart/form-data"); 

然后让curl为您设置。 (将CURLOPT_POSTFIELDS设置为array时,curl会自动以正确的边界设置该标头。)

第二次,您不是在这里发送实际文件,而是在发送文字字符串

new \CURLFile(realpath($filedata))

..如果要发送$ filedata指向的文件,请执行

$data_array =  array(
  "jobname" => "test",
  "file" => new \CURLFile(realpath($filedata))
);

相反。