Add Media with API in PHP - Error Filetype Not Allowed



I want to upload a media with the api with php.
Upload a media with postman is actually working but when I try to do it in php, I always get a Filetype not allowed error :
array(1) {
object(stdClass)#5671 (4) {
string(15) “1536761420-4641”
string(19) “multipart/form-data”
string(20) “Filetype not allowed”

My code, inpired by this implementation:

$file = file_get_contents($filepath);
//$file = fopen($filepath, ‘r’);
$body = array(
‘multipart’ => [
‘name’ => ‘name’,
‘contents’ => ‘API upload test’
‘name’ => ‘files’,
‘contents’ => $file
‘headers’ => array(“Authorization” => "Bearer ".$token, “Content-type” => “multipart/form-data”),
$response = wp_remote_post( get_option(‘xibo_cms_api_url’) . ‘/library’, $body );

In this code, I use wordpress customs functions but I’ve tried without in a alone php file using this as base and I had the same error.

Do you have a idea of what I’m doing wrong ?



We do have a file upload example using guzzle here -

Alternatively you could also use our ouath2 api library namely this wrapper -
so then you could just call use this create wrapper and pass the media name and path to the file (and if replacing then oldMediaId, updateInLayouts, deleteOldRevisions)
which would make it a bit more straightforward (and you’d use one line of the code to do it.