Reputation: 33
I try to make a PUT request with curl. I need to send a csv file with stock update. When I execute my script I get the response code 200 but the file is not sending. The csv file is in the same directory.
<?php
try {
$file_name= "file_test.csv";
$api_key= "my-key";
$Client_id= "my-id";
$ch = curl_init("https://merchants-connector-importer.zalandoapis.com/".$Client_id."/".$file_name);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = array('file' =>curl_file_create($file_name, 'text/csv'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
"x-api-key: ".$api_key,
"Content-Type: application/csv",
"cache-control: no-cache"));
$content = curl_exec($ch);
if ($content === false) {
echo "ERROR";
throw new Exception(curl_error($ch), curl_errno($ch));
}else{
var_dump($content);
$decodedJson = json_decode($content, true);
var_dump(http_response_code());
}
} catch (Exception $e) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
Upvotes: 0
Views: 1371
Reputation: 33
When I execute my code local it works fine. But when I try to execute it from my server it does not work anymore.
<?php
try {
$file_name= "test-file.csv";
$api_key= "my-key";
$Client_id= "my-id";
$file =fopen(__DIR__."/".$file_name, "r");
$ch = curl_init("https://merchants-connector-importer.zalandoapis.com/".$Client_id."/".$file_name);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_name));
curl_setopt($ch,CURLOPT_HTTPHEADER, array(
"x-api-key: ".$api_key,
"Content-Type: text/csv",
"cache-control: no-cache"));
$content = curl_exec($ch);
if ($content === false) {
echo "ERROR";
throw new Exception(curl_error($ch), curl_errno($ch));
}else{
var_dump($content);
$decodedJson = json_decode($content, true);
var_dump(http_response_code());
}
} catch (Exception $e) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
Upvotes: 0
Reputation: 9135
Referring to the docs you do not need CurlFile in the CURLOPT_POSTFIELDS for that, because it is no POST request. I've also noticed the content-type for CURLOPT_HTTPHEADER should be the mime type text/csv
instead of application/csv
and CURLOPT_UPLOAD is set to true.
The file to PUT must be set with CURLOPT_INFILE and CURLOPT_INFILESIZE.
Not tested, but I hope you get the right hint out of it.
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_UPLOAD, true);
curl_setopt($ch, CURLOPT_INFILE, $file_name);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_name));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"x-api-key: " . $api_key,
"Content-Type: text/csv"
]);
$content = curl_exec($ch);
Upvotes: 1