【问题标题】:PHP Curl upload file using form-data always missing parametersPHP Curl使用表单数据上传文件总是缺少参数
【发布时间】:2015-06-23 22:15:44
【问题描述】:

需要帮助,我一直在寻找这个,但仍然不知道为什么仍然会出错

我正在使用 php n curl 将文件上传到 API

html表单

<form enctype="multipart/form-data" method="post" action="/function/stickerGroup/postStickerGroup.php" id="addStickerGroup" class="form-horizontal">
<div class="form-group has-feedback">
      <label class="col-sm-2 control-label">Image</label>
      <div class="col-sm-5">
         <input type="file" class="form-control" name="image" placeholder="Choose Photo..">
      </div>
</div> 
<div class="form-group">
  <label for="" class="col-sm-2"></label>
     <div class="col-sm-3">
         <input type="submit" name="" value="Add Sticker" class="btn btn-info btn-block">
     </div>
</div>
</form>

postStickerGroup.php

$filename = $_FILES['image']['name'];
$filedata = $_FILES['image']['tmp_name'];
$filesize = $_FILES['image']['size'];

    $url = test.net/api/sticker-group;
    $token = getToken();
    $headers = array("Content-Type:multipart/form-data",'Api-Key: '.$token); 
    $postfields = array("filedata" => '@'.$filedata, "filename" => $filename);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => true,
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $options);
  $response = curl_exec($ch);

  curl_close($ch); 

它一直给我错误

Deprecated: curl_setopt_array(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead in /home/mamypoko/public_html/function/network/base.php on line 118
HTTP/1.1 100 Continue HTTP/1.1 200 OK Server: nginx/1.4.6 (Ubuntu) Date: Fri, 17 Apr 2015 07:00:44 GMT Content-Type: application/json; charset=utf-8 Content-Length: 53 Connection: keep-alive {"code":400,"status":"Invalid or missing parameters"}

需要帮助通过表单数据上传文件的示例代码

【问题讨论】:

    标签: php curl


    【解决方案1】:

    基本 curl POST 请求:

      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_ENCODING,"");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HEADER, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
      curl_setopt($ch, CURLOPT_TIMEOUT,100);
      curl_setopt($ch, CURLOPT_FAILONERROR,true);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
      $response = curl_exec($ch);
    

    您真的想查看 HTTP 响应标头吗?

    【讨论】:

    • 是的,我需要一些带有 http 响应头的东西,你能帮我在 php 中使用 form-data 进行 curl 发布和上传文件吗?
    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多