Upload file using API

I want to upload a file using the API in PHP.

Here is what I have done :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/files/remote.php/dav/files/$username/Folder/test.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password );

$post = array(
    'file' => '@' .realpath('\"../../data/test.txt\"')
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    $result = ["Error" => "Can't etablish connection"];

Result of var_dump();

string(0) ""

The credentials are correct. The folder exist.
I’m able to establish a succesful connection.

What is the exact error code of the curl command execution? Eventually it helps to increase the verbosity, like described here.