PHP POST API未收到文件

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (35)

我们的团队正在开发一个用于远程访问PHP打印机的Web应用程序。我们尝试使用multipart / form-data实现POST print_job部分,但它不起作用,它显示没有收到文件。此API将检查ID和密钥。这是代码。我想知道如何定义$ filedata以使.gcode文件可读。任何帮助表示赞赏!

我检查id,key和.gcode是否正确。它运行在Apache 2.4.39,PHP 7.3.5,XAMPP控制面板3.2.2上。所有其他部分包括PUT API工作。

细节是:

POST API

  <div class="row">
    <form action="server/print.php" method="post" enctype="multipart/form-data">
      <label class="col-sm-2" for="file">Choose a file :</label>
      <input type="hidden" name="jobname" value="test">
      <input class="col-sm-8" type="file" name="file" id="file">
      <input class="col-sm-2 btn btn-primary" type="submit" value="Print">
    </form>

  </div>

卷筒纸

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

  switch ($method){
     case "POST":
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data)
           //curl_setopt($curl,CURLOPT_SAFE_UPLOAD,false);
           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_HTTPHEADER, array(
    'Content-Type: application/json'
 ));
  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');
?>

我希望3d打印机可以接收文件,然后工作。

提问于
用户回答回答于

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

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

所以删除这个:

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

让curl为你设置它。(当将CURLOPT_POSTFIELDS设置为数组时,curl将自动设置具有正确边界的标题。​​)

第二,你没有在这里发送实际文件,你只是发送文字字符串

new \CURLFile(realpath($filedata))

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

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

代替。

扫码关注云+社区

领取腾讯云代金券