PHPを使用して単純な画像ファイルをFedoraCommonsに取り込もうとしていますが、それを機能させることができません(新しい空のオブジェクトにデータストリームをアタッチしようとすると、Fedora Commonsは500を返します)。
この質問の最後にコード全体を投稿しましたが、アイデアを得るための疑似コードを次に示します。
ユーザーが自分のコンピューターでファイルを選択して送信ボタンを押すと、スクリプトが呼び出され、...
- ファイルを一時ディレクトリにアップロードします(にアクセスすると、その画像を確認できます
http://localhost/drupal/sites/default/files/images/singe_6.jpg
) - 新しい空のFedoraCommonsオブジェクトを作成します(にアクセスすると、そのオブジェクトを確認できます
http://myFedoraServer:8082/fedora/objects/some%3Apid
) - cURLを使用して次のURLにPOSTすることにより、ファイルをデータストリームとして空のオブジェクトに添付します。
http://myFedoraServer:8082/fedora/objects/some:pid/datastreams/myDatastreamID?controlGroup=M&dsLocation=http://localhost/drupal/sites/default/files/images/singe_6.jpg
- Fedoraサーバーから500エラー応答を受信し、エラーメッセージを表示して終了します
その他の試み
- ファイルへのパス( )を(ではなく)
dsLocation
相対パスに変更します。何も変更しません。/sites/default/files/images/singe_6.jpg
http://localhost/drupal/sites/default/files/images/singe_6.jpg
- ファイルへのパス(
dsLocation
)をインターネット上のランダムな画像への絶対パス(http://http://colibri45.c.o.pic.centerblog.net/cv369byr.jpg
ではなくhttp://localhost/drupal/sites/default/files/images/singe_6.jpg
)に変更しても、何も変更されません。
私はここで何が間違っているのですか?インスピレーションとして使用できるサンプルスクリプトはありますか?
コード
これが私のコードです。drupalでアップロードファイルフォームを作成し、そのファイルをFedoraCommonsに保存しようとします。
<?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");
}
}
?>
結果
空のオブジェクトの作成に成功しました。PID:personne:myObjectID
ユーザーエラー:(0)
curl_getinfo(...)
が予期しないhttpコードを返しました(201を期待していましたが、500を取得しました)。エラー: 。完全な情報:配列([url] => http://vitdevelapp-cen.cen.umontreal.ca:8082/fedora/objects/personne:myObjectID/datastreams/myDatastreamID?controlGroup=M&dsLocation=http://localhost/drupal/ sites / default / files / images / singe_6.jpg [content_type] => [http_code] => 500 [header_size] => 215 [request_size] => 330 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.602249 [namelookup_time ] => 1.9E-5 [connect_time] => 0.005847 [pretransfer_time] => 0.005849 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 0 [ upload_content_length] => -1 [starttransfer_time] => 0.602222 [redirect_time] => 0 [certinfo] => Array())dans curlThis()(ligne 100 dans / var / www / drupal / sites / all / modules / editChercheur / fedora_test.php)。
Fedoraログから
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
(Connexion refusée
に変換されConnection refused
ます)