0

I'm using the below code to upload an MP4 file to a web service, using PHP cURL.

I've specified the 'Content-Type' as 'video/mp4', in CURLOPT_HTTPHEADER.

Unfortunately, having uploaded the file, the 'Content-Type' stored for it in the service displays as: "content_type":"video/mp4; boundary=----WebKitFormBoundaryfjNZ5VkJS8z3CB9X"

As you can see, the 'boundary' has been inserted into the 'content_type'.

When I then download the file, it fails to play, with a 'file unsupported/file extension incorrect/file corrupt' message.

$authorization = "Authorization: Bearer [token]"; 

$args['file'] = curl_file_create('C:\example\example.mp4','video/mp4','example');

$url='[example web service URL]';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', 'Accept: application/vnd.mendeley-content-ticket.1+json', $authorization)); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS , $args);

$response = curl_exec($ch); // URL encoded output - needs to be URL encoded to get the HREF link header
curl_close($ch);

Would be extremely grateful for any help, advice or pointers!

Michael Jones
  • 80
  • 1
  • 7

2 Answers2

1

Maybe the API doesn't expects a POST multipart, but the actual contents in the body itself:

Ref: How to POST a large amount of data within PHP curl without memory overhead?

You need to use PUT method for the actual contents of the file to go inside the body - if you use POST, it will try to send as a form.

$authorization = "Authorization: Bearer [token]"; 
$file = 'C:\example\example.mp4';
$infile = fopen($file, 'r');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            "https://api.mendeley.com/file_contents");
curl_setopt($ch, CURLOPT_PUT,            1 ); // needed for file upload
curl_setopt($ch, CURLOPT_INFILESIZE,     filesize($file));
curl_setopt($ch, CURLOPT_INFILE,         $infile);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,  'POST' );
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: video/mp4', 'Accept: application/vnd.mendeley-content-ticket.1+json', $authorization)); 

curl_setopt($ch, CURLOPT_HEADER,         0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec ($ch);
Daniel Costa
  • 380
  • 1
  • 9
  • Thank you so much, this solved it! Working perfectly now :) – Michael Jones Apr 04 '20 at 17:38
  • This worked for me for years. Now the server reports "Bad Request 400". I had to add `sprintf('Content-Length: %d', filesize($file))` to the httpheader array to get it working again. – daniel Nov 17 '22 at 11:45
0

I have the same problem calling a Openai API from PHP curl. I send options: Content-Type and Authorization (with my api key) but, when I send request, I receive this error:

Invalid Content-Type header (application/json; boundary=------------------------66a0b850cd1421c8), expected application/json

I tried to use some of your options with no success. I cannot remove the boundary parameter added automatically.

Disu
  • 113
  • 10
  • I had the boundary issue because I was not json-encoding the CURLOPT_POSTFIELDS data array. – mmrtnt Apr 07 '23 at 17:05