REST API v4: POST medialibrary files
Je kunt een bestand/afbeelding toevoegen aan een mediabibliotheek door een HTTP POST-request te sturen naar de volgende URL:
https://api.copernica.com/v4/medialibrary/$id/files
De code $id moet je vervangen door de numerieke identifier of de naam van de mediabibliotheek waar je het bestand of afbeelding in wilt opslaan.
Bestand uploaden
Om bestanden te uploaded naar de REST API kunt je HTTP POST-requests gebruiken. Maar let op, het content-type van de calls moet, in tegenstellng tot de andere POST-calls, staan ingesteld op "multipart/form-data". De body data van het request moet ook in dit multipart formaat worden verstuurd (en dus niet in JSON of application/x-www-form-urlencoded formaat zoals bij de andere POST-calls).
De naam die je aan de variabelen geeft is niet relevant. In onze voorbeelden gebruiken we "file", maar dit kan dus van alles zijn. Het content-type dat je meegeeft aan het bestand kan wel relevant zijn, omdat afbeeldingen anders kunnen worden verwerkt dan reguliere bestanden. Dit is met name relevant bij uploads naar een media library.
Indien je gebruik maakt van PHP en CURL dan regelt CURL dit allemaal voor je. Door een CURL-call te doen waar CURLFile objecten in worden gebruikt, schakelt PHP/CURL vanzelf over op de multipart-encoding.
Voorbeeld in PHP
Het volgende PHP-script demonstreert hoe je de API-methode kunt aanroepen:
// your API access token from Marketing Suite
$token = 'XXX';
// the file to upload (with absolute path)
$file = "/home/path/to/file.ext";
// ID of the media library where the file should be uploaded
$medialibraryId = 4;
// connect to the authentication server
$ch = curl_init("https://authenticate.copernica.com");
// use a POST request
curl_setopt($ch, CURLOPT_POST, true);
// authenticate using your API access token
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$token}"]);
// save the temporary authentication token (JWT) for the upload
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// treat HTTP errors as failed requests
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// retrieve the temporary token and stop if authentication fails
if (($jwt = curl_exec($ch)) === false) exit("Authentication failed: ".curl_error($ch).PHP_EOL);
// close the connection to the authentication server
curl_close($ch);
// the API endpoint for the selected media library
$url = "https://api.copernica.com/v4/medialibrary/{$medialibraryId}/files";
// connect to the endpoint
$ch = curl_init($url);
// use a POST request
curl_setopt($ch, CURLOPT_POST, true);
// authorize the upload using the temporary token
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$jwt}"]);
// attach the file; cURL automatically handles the upload format
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => new CURLFile($file, mime_content_type($file))]);
// treat HTTP errors as failed requests
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// upload the file and stop if the upload fails
if (curl_exec($ch) === false) exit("Upload failed: ".curl_error($ch).PHP_EOL);
// close the connection
curl_close($ch);
Dit voorbeeld vereist de REST API-klasse.