使用表单数据的PHP Curl上传文件总是缺少参数

时间:2015-04-17 07:23:29

标签: php curl

需要帮助,我一直在搜索这个,但仍然不知道为什么还会收到错误

我正在使用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"}

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

1 个答案:

答案 0 :(得分:0)

基本卷曲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响应标头吗?