php-mod / curl

This library provides an object-oriented and dependency free wrapper of the PHP cURL extension.
MIT License
328 stars 120 forks source link

Post files does not work (php7.1) #46

Closed danielsetreus closed 5 years ago

danielsetreus commented 7 years ago

POSTing files does not work as intended.

This is what I'd like to do:

$file = new \CURLFile($filePath, mime_content_type($filePath), 'file_post_name');
$data = array(
    'mode' => 'import',
    'file' => $file,
);
$this->curl->setHeader('Accept', 'application/json');
$this->curl->setHeader('Content-Type', 'application/json');
$this->curl->post('http://example.com', $data);
$this->curl->close();

However what is posted is a string representation of the data, including the CURLFile instance, not the actual file. I.e:

mode=import&file%5Bname%5D=path/to/file/&file%5Bmime%5D=mime_type_of_file&file%5Bpostname%5D=file_post_name

This is due to the preparePayload method - that runs http_build_query on the data. If I remove those lines everything works as expected.

The library should include a way to skip http_build_query, or a special method for adding a file to the posted data.

Thanks

worka commented 6 years ago

Faced the same problem

nadar commented 6 years ago

@danielsetreus We could isolate the array keys which are an instanceof \CURLFile and add them after processing the http_build_query() data?

nadar commented 5 years ago

@danielsetreus https://github.com/php-mod/curl/pull/53/files this should fix the problem - right?