【问题标题】:Image is not uploading to the server with REST API using PHP cURL图像未使用 PHP cURL 通过 REST API 上传到服务器
【发布时间】:2020-01-20 14:33:24
【问题描述】:

我正在尝试通过 REST API 将图像上传到服务器。所以我使用了 PHP cURL。但是我遇到文件没有上传到服务器端。我在哪里做错了?请帮忙。下面是我的代码

<?php

$resp = "";
//check is POST
if (isset($_POST['submit'])) {
    //check image upload, your want to check for other things too like: is it an image?
    if(isset($_FILES['file']['name'])){
        //make filename for new file
        $uploadfile = basename($_FILES['file']['name']);
        //move the upload
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "http://example.com/",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => array('file'=> dirname(__FILE__).'/'.$uploadfile),
                CURLOPT_HTTPHEADER => array(
                  "Content-Type: multipart/form-data; boundary=--------------------------516718006976498379520930"
                ),
            ));

            $resp = curl_exec($curl);
            curl_close($curl);
        }
    }
    else {
        $resp = "Upload a valid image file";
    }
}
?>

    <form runat="server" id="form" enctype="multipart/form-data" method="POST" action="">
        <div class="upload">
            <div id="upload-image">Upload Image</div>
            <input type="file" name="file" id="file">
        </div>submit" name="submit" id="main" value="Match Face"></input>
    </form>

【问题讨论】:

  • 停止尝试添加您自己的 Content-Type 标头。 cURL 不知道您在其中指定的边界值,它自己创建。基本上,您这样做是对接收者谎称您的数据实际上是如何构造的。
  • 另外,您并没有首先发送实际的文件内容。 CURLOPT_POSTFIELDS =&gt; array('file'=&gt; dirname(__FILE__).'/'.$uploadfile) - 仅是文件的本地路径,在参数名称file 下发送。去阅读php.net/manual/en/curlfile.construct.php 了解它是如何工作的。
  • 感谢您的评论,我正在学习 cURL PHP 以及我从 Postman 获得的这些代码。我什至添加了代码new CURLFILE,但它也不起作用。

标签: php rest api curl


【解决方案1】:

根据您的代码,只需使用以下代码。我希望它可以工作。

$ch = curl_init('http://example.com/');
curl_setopt($ch, CURLOTP_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => new CURLFile(dirname(__FILE__).'/'.$uploadfile)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);

让我知道它是否有效。

【讨论】:

  • @ShirshaGhosh 编码快乐
【解决方案2】:

您将文件上传到本地并在服务器中发送本地地址 在服务器文件中,您必须在本地而不是在本地编写代码

<?php
  if(isset($_FILES['file']['name'])){
$picurl = $this->UploadPic($_FILES['file']['name']);
}
      publick function UploadPic($file){
//your uploader code here
//at the end you must return address
}

?>

【讨论】:

  • 是的,我实际上也在做同样的事情
  • 在您的 api 文件中,您必须将图像传递到服务器。删除 move_uploaded_file 和 `` CURLOPT_POSTFIELDS => array('file'=> dirname(FILE)。 '/'.$uploadfile),``` 你必须像这样编码 ``` CURLOPT_POSTFIELDS => array('file'=> $_FILES['file']['tmp_name']), ```
猜你喜欢
  • 2019-01-25
  • 2012-07-24
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多