如何使用PHP将内容吸收到Fedora Commons中?
我试图使用PHP将一个简单的图像文件摄取到Fedora Commons中,但我无法使其工作(当我尝试将数据流附加到新的空对象时,Fedora Commons返回500) 我已经在这个问题的末尾发布了我的全部代码,但这里有一些伪代码只是为了得到这个想法: 当用户在他的计算机上选择一个文件并按下submit按钮时,我的脚本将被调用如何使用PHP将内容吸收到Fedora Commons中?,php,curl,fedora-commons,Php,Curl,Fedora Commons,我试图使用PHP将一个简单的图像文件摄取到Fedora Commons中,但我无法使其工作(当我尝试将数据流附加到新的空对象时,Fedora Commons返回500) 我已经在这个问题的末尾发布了我的全部代码,但这里有一些伪代码只是为了得到这个想法: 当用户在他的计算机上选择一个文件并按下submit按钮时,我的脚本将被调用 将文件上载到临时目录(我可以通过访问http://localhost/drupal/sites/default/files/images/singe_6.jpg) 创建
- 将文件上载到临时目录(我可以通过访问
)http://localhost/drupal/sites/default/files/images/singe_6.jpg
- 创建一个新的空Fedora Commons对象(我可以通过访问
)http://myFedoraServer:8082/fedora/objects/some%3Apid
- 通过使用cURL将文件作为数据流附加到空对象,以发布到以下URL:
http://myFedoraServer:8082/fedora/objects/some:pid/datastreams/myDatastreamID?controlGroup=M&dsLocation=http://localhost/drupal/sites/default/files/images/singe_6.jpg
- 从Fedora服务器接收500错误响应,显示错误消息并退出
org.fcrepo.server.errors.GeneralException: Error getting http://localhost/drupal/sites/default/files/images/singe_6.jpg
Caused by: java.net.ConnectException: Connexion refusée
- 将文件路径(
)更改为相对路径(dsLocation
,而不是/sites/default/files/images/singe_6.jpg
)。什么都不会改变http://localhost/drupal/sites/default/files/images/singe_6.jpg
- 将文件路径(
)更改为指向internet上随机图片的绝对路径(dsLocation
而不是http://http://colibri45.c.o.pic.centerblog.net/cv369byr.jpg
)不会改变任何东西http://localhost/drupal/sites/default/files/images/singe_6.jpg
代码 下面是我的代码,它在drupal中创建一个上传文件表单,并尝试将该文件保存到Fedora Commons:
<?php
// Create the form with drupal's form api
function fedora_test($form, $form_state) {
$form = array(
'#attributes' => array(
'enctype' => 'multipart/form-data'
),
'fichier' => array(
'#tree' => false,
'#type' => 'file'
),
'enregistrer' => array(
'#type' => 'submit',
'#value' => t('Enregistrer'),
'#submit' => array('fedora_test_enregistrer')
)
);
return $form;
}
// Validate the form data before going on to the submission function (see fedora_test_enregistrer)
function fedora_test_validate($form, &$form_state) {
$validators = array(
"file_validate_extensions" => array(variable_get("allowed_extensions")),
"file_validate_size" => array(variable_get("max_image_size"))
);
$file = file_save_upload('fichier', $validators, variable_get("uploaded_files_destination"));
if ($file !== false && $file !== null) {
$form_state['file_storage'] = $file;
} else {
form_set_error('fichier', "Impossible de charger le fichier");
}
}
// Use cURL with the provided functions and return the result if the HTTP Code recieved matches the expected HTTP Code
function curlThis($curlOptions, $expectedHttpCode) {
$returnValue = false;
try {
$curlHandle = curl_init();
if ($curlHandle === false) {
throw new Exception(
"`curl_init()` returned `false`"
);
}
$settingOptionsSucceeded = curl_setopt_array($curlHandle, $curlOptions);
if ($settingOptionsSucceeded === false) {
throw new Exception(
sprintf(
"`curl_setopt_array(...)` returned false. Error: %s. Info: %s",
curl_error($curlHandle),
print_r(curl_getinfo($curlHandle), true)
),
curl_errno($curlHandle)
);
}
$curlReturn = curl_exec($curlHandle);
if ($curlReturn === false) {
throw new Exception(
sprintf(
"`curl_exec(...)` returned false. Error: %s. Info: %s",
curl_error($curlHandle),
print_r(curl_getinfo($curlHandle), true)
),
curl_errno($curlHandle)
);
}
$httpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
if ($httpCode === false) {
throw new Exception(
sprintf(
"`curl_getinfo(...)` returned false. Error: %s.",
curl_error($curlHandle)
),
curl_errno($curlHandle)
);
}
if ($httpCode !== $expectedHttpCode) {
throw new Exception(
sprintf(
"`curl_getinfo(...)` returned an unexpected http code (expected %s, but got %s). Error: %s. Complete info: %s",
$expectedHttpCode,
$httpCode,
curl_error($curlHandle),
print_r(curl_getinfo($curlHandle), true)
),
curl_errno($curlHandle)
);
}
$returnValue = $curlReturn;
} catch (Exception $e) {
trigger_error(
sprintf(
"(%d) %s",
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
return $returnValue;
}
// Create a new empty object in Fedora Commons and return its pid
function createNewEmptyObject($prefix, $id) {
$returnValue = false;
// Build URL
$protocol = variable_get("fedora_protocol");
$host = variable_get("fedora_host");
$port = variable_get("fedora_port");
$context = variable_get("fedora_context");
$pid = $prefix . ":" . $id;
$url = sprintf(
"%s://%s:%d/%s/objects/%s",
$protocol,
$host,
$port,
$context,
$pid
);
// Build cURL options
$userPassword = variable_get("fedora_username") . ":" . variable_get("fedora_password"); // username:password
$verifyPeer = false; // false for ignoring self signed certificates
$headers = array("Accept: text/xml", "Content-Type: text/xml");
$curlOptions = array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => $userPassword,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_SSL_VERIFYPEER => $verifyPeer,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true
);
// Try `cURL`ing
$result = curlThis($curlOptions, 201);
if ($result === $pid) {
$returnValue = $result;
}
return $returnValue;
}
function attachDatastream ($pid, $file, $datastreamID) {
$returnValue = false;
// Build URL
$protocol = variable_get("fedora_protocol");
$host = variable_get("fedora_host");
$port = variable_get("fedora_port");
$context = variable_get("fedora_context");
$url = sprintf(
"%s://%s:%d/%s/objects/%s/datastreams/%s?controlGroup=M&dsLocation=%s",
$protocol,
$host,
$port,
$context,
$pid,
$datastreamID,
file_create_url($file->uri)
);
drupal_set_message("url: " . $url, 'warning');
// Build cURL options
$userPassword = variable_get("fedora_username") . ":" . variable_get("fedora_password"); // username:password
$verifyPeer = false; // false for ignoring self signed certificates
$headers = array("Accept: text/xml", "Content-Type: text/xml");
$curlOptions = array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERPWD => $userPassword,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_SSL_VERIFYPEER => $verifyPeer,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true
);
// Try `cURL`ing
$result = curlThis($curlOptions, 201);
if ($result === $pid) {
$returnValue = $result;
}
return $returnValue;
}
function fedora_test_enregistrer($form, &$form_state) {
$pid = createNewEmptyObject("personne", "myObjectID");
if ($pid) {
drupal_set_message("Creating empty object succeeded. PID: " . $pid);
$result = attachDatastream($pid, $form_state['file_storage'], "myDatastreamID");
if ($result) {
drupal_set_message("Attaching a datastream to pid " . $pid . " succeeded!");
} else {
form_set_error("FAILED ATTACHING DATASTREAM TO NEW OBJECT");
}
} else {
form_set_error("FAILED CREATING NEW EMPTY OBJECT");
}
}
?>
(
Connexion refusée
翻译为连接被拒绝
)Fedora日志中的500条错误消息文本是什么?Fedora会因为许多愚蠢的原因抛出错误,但是如果没有错误消息文本,就很难开始调试这个问题
我的第一个猜测是,“singe_6.jpg”图像可能不存在,或者Fedora服务器无法访问。“localhost”上的映像路径是否与Fedora服务器vitdevelapp cen.cen.umontreal.ca相同?Fedora日志中的500错误消息文本是什么?Fedora会因为许多愚蠢的原因抛出错误,但是如果没有错误消息文本,就很难开始调试这个问题
我的第一个猜测是,“singe_6.jpg”图像可能不存在,或者Fedora服务器无法访问。“localhost”上的映像路径是否与Fedora服务器vitdevelapp cen.cen.umontreal.ca相同?有人能帮我编辑这个问题吗?我对Fedora Commons的世界有点陌生,我怀疑一些术语可能有点不对劲……有人能帮我编辑一下这个问题吗?我对Fedora Commons的世界有点陌生,我怀疑一些术语可能有点偏离……我不确定我是否理解最后一个问题。我正在本地(localhost)测试我的drupal模块,而Fedora则托管在另一台服务器(vitdevelapp cen.cen.umontreal.ca)上。我尝试使用互联网上已有的映像,而不是我自己上传的映像,但这并没有改变任何事情。查看我的上次编辑。日志似乎表明fedora无法获取我的图像,因为连接被拒绝。。。再次查看我的编辑。我不知道该怎么办,因为无论我尝试发送哪个图像,我总是收到相同的错误消息。这回答了一些问题。首先,将URL中的“localhost”更改为Drupal计算机的实际IP地址或名称。其次,使用CURL或类似程序:CURL”“>test.jpg验证您是否确实可以从Fedora服务器下载该文件。如果失败,则可能有两个原因:1-Drupal服务器上不存在该文件(根据错误可能存在)或者2-可能有防火墙阻止Fedora服务器下载文件。事实上,Fedora服务器似乎无法访问drupal服务器上的文件,也无法访问internet上的其他任何地方。解决方案似乎是将文件本身与请求一起传递。不过,我对这种方法也有困难,我不确定我是否理解最后一个问题。我正在本地(localhost)测试我的drupal模块,而Fedora则托管在另一台服务器(vitdevelapp cen.cen.umontreal.ca)上。我尝试使用互联网上已有的映像,而不是我自己上传的映像,但这并没有改变任何事情。查看我的上次编辑。日志似乎表明fedora无法获取我的图像,因为连接被拒绝。。。再次查看我的编辑。我不知道该怎么办,因为无论我尝试发送哪个图像,我总是收到相同的错误消息。这回答了一些问题。首先,将URL中的“localhost”更改为Drupal计算机的实际IP地址或名称。其次,使用CURL或类似程序:CURL”“>test.jpg验证您是否确实可以从Fedora服务器下载该文件。如果失败,则可能有两个原因:1-Drupal服务器上不存在该文件(根据错误可能存在)或者2-可能有防火墙阻止Fedora服务器下载文件。事实上,Fedora服务器似乎无法访问drupal服务器上的文件,也无法访问internet上的其他任何地方。解决方案似乎是将文件本身与请求一起传递。不过,我在这种方法上也遇到了麻烦,明白吗