Michael Chamunorwa Michael Chamunorwa - 4 months ago 26
PHP Question

fedora commons ingest object via REST error

I recently set up FEDORA for a project I am working on to catalogue various
media. I want to be able to consume files (datastreams) via the FEDORA REST api. I managed to create a digital object via curl with no issues at all. I also managed to add an html page as a datastream to the digital object mentioned above with no problems as well.

However, adding a digital object with other content types/file types fails and throws an internal server error 500. On checking the logs, the following error appears:

[http-bio-8080-exec-18] (DatastreamResource) Error with uploaded://47 : XML was not well-formed. Invalid byte 1 of 1-byte UTF-8 sequence

The following is my code snippet of how I am ingesting the files:

$url = "http://localhost:8080/fedora/objects/changeme:5/datastreams/NEWDS8?controlGroup=X&dsLabel=LAZLO";
$file = "namibia2015.pdf";
// Build cURL options
$userPassword = "fedoraAdmin:test123"; // username:password
$verifyPeer = false; // false for ignoring self signed certificates
$headers = array("Accept: text/xml", "Content-Type: " . mime_content_type($file));
$fileContents = file_get_contents($file);
$curlOptions = array(
CURLOPT_URL => $url,
CURLOPT_USERPWD => $userPassword,
$curlHandle = curl_init();
$success = curl_setopt_array($curlHandle, $curlOptions);
throw new Exception(
"curl_setopt_array(...) failed. Error: %s. Info: %s",
print_r(curl_getinfo($curlHandle), true)
$curlReturn = curl_exec($curlHandle);
$httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);

I came across this post How can I ingest an image into Fedora Commons using PHP? tried the suggested method but still no luck.

What am I doing wrong? What am I missing? Why is it possible to add an html file datastream to the digital object but it fails when I try to
add .jpeg, .pdf, .txt etc?


I finally fixed the error. The exception was being caused by the way I was structuring my URL parameters in my curl request. Using a URL with the following format:

$url = "http://localhost:8080/fedora/objects/changeme:5/datastreams/NEWDS8?controlGroup=X&dsLabel=LAZLO";

will throw the error. Instead, you have to build an http query of all the options you want attached to the POST request. I did that as follows:

$array = array();
$array['dsID'] = '5' ;
$array['controlGroup'] = 'M' ;
$array['altIDS'] = 'Other';
$array['versionable'] = true;
$array['dsLabel'] = 'The pic';
$array['logMessage'] = 'Example log message';

$link = "http://localhost:8080/fedora/objects/changeme:5/datastreams/newobject";
$params = http_build_query($array);
$url = $link.'?'.$params; //add the http query parameters to the url

Thereafter, I made my curl request as before and it will successfully create a data stream attached to the digital object.

Hope this will help someone in the future.