lundi 29 août 2016

cURL csv file submission returning OK status but no data is uploaded

I'm having difficulty uploading a .csv file through a cURL request to a RESTful API. I'm receiving a successful response (200 OK) but no data appears to be submitted:

{
  "meta": [
  ],
  "code": 200,
  "object": "csvfile",
  "data": [
  ],
  "time": 1472464675
}

This is being made with the following request. I've added commenting to break down each of the CURL_SETOPTS:

    // create a CURLFile object
    $cfile = curl_file_create($fileName,'text/csv', $fileName);

    // start curl request
    $curl = curl_init();

    // assign POST data
    $data = array('file' => $cfile);

    // specify POST request
    curl_setopt($curl, CURLOPT_POST, true);

    // basic auth
    curl_setopt($curl, CURLOPT_USERPWD, getenv('USERNAME').':'.getenv('PASSWORD'));

    // destination URI
    curl_setopt($curl, CURLOPT_URL, 'http://ift.tt/2c2bzlv');

    // associate curlfile
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    // since we're using PHP 5.6 we need to enable CURLOPT_SAFE_UPLOAD
    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);

    // return options
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    curl_setopt($curl, CURLOPT_STDERR, $out);

    $response = curl_exec($curl);
    $err = curl_error($curl);

    // debugging
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo("CURL RESPONSE: ");
        echo $response;
    }
    curl_close($curl);

I should mention I don't fully understand the importance of the $postname field in CURLfile objects. Furthermore I'm not entirely sure what I'm doing by assign POST data as demonstrated in the construct's documentation (http://ift.tt/1YZS8eC).

Note: I'm writing this in a Laravel application and am aware of the Guzzle client but have opted to present the problem this way, as there's more support for typical cURL problems on SO. I believe the issue is something to do with my cURLfile creation but have spent hours trying to pinpoint it.

What could be causing this issue?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire