0

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
    );
}

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

2 Comments

why do we need api key, I don't have any api_key. Please tell me how to use this without api key
@GireeshDoddipalli The OP used that and I corrected his code. If you need a key, ask the API vendor. Most have a (dev) login where to obtain it.
0

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
    );
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.