php - cURL csv file submission returning OK status but no data is uploaded -
i'm having difficulty uploading .csv file through curl request restful api. i'm receiving successful response (200 ok) no data appears submitted:
{ "meta": [ ], "code": 200, "object": "csvfile", "data": [ ], "time": 1472464675 }
this being made following request. i've added commenting break down each of curl_setopts
:
// create 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, 'https://api.arestfulapi.com'); // associate curlfile curl_setopt($curl, curlopt_postfields, $data); // since we're using php 5.6 need 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 don't understand importance of $postname
field in curlfile objects. furthermore i'm not entirely sure i'm doing assign post data
demonstrated in construct's documentation (http://php.net/manual/en/class.curlfile.php).
note: i'm writing in laravel application , aware of guzzle client have opted present problem way, there's more support typical curl problems on so. believe issue curlfile creation have spent hours trying pinpoint it.
what causing issue?
i found solution . reworked request using guzzle client again , had success. here's copy of solution:
// instantiate guzzle http request $client = new \guzzlehttp\client(); // provide fopen resource $filepath = 'public/'.$filename; $body = fopen($filename, 'r'); // make request $res = $client->request('post', 'https://api.website.com', [ 'auth' => [ getenv('username'), getenv('password') ], 'body' => $body ]); echo ("status code: "); echo $res->getstatuscode(); // 200
Comments
Post a Comment