How can I "correctly" upload the content of a file with cURL?
There are a lot of explanations and questions here where tmp files are send by curl with a @prefix on the file or using the curlFile api. In My use case I don't want to create a tmp file, and just post the content of file_get_contents('php://input') as body of the post request. The server does not accept multipart form requests.
The Following snipped will work in PHP7:
$body = file_get_contents('php://input'); $ch = curl_init(); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); It feels ugly to set the content type to json, no matter what the actual content of the stream may holds. But it seems to work. Sadly I need to support a really old version of PHP and feel like running out of options.
My question is: Is the above example correct, even for PHP 7 or can it be made better? Additionally: Is there any improvement or option that can be used to make this work with PHP 5.3.10? In this old version the post body always seems to be empty.