1

我正在尝试使用 PHP 将一个简单的图像文件摄取到 Fedora Commons,但我无法让它工作(当我尝试将数据流附加到我的新空对象时,Fedora Commons 返回 500)。

我已经在这个问题的末尾发布了我的整个代码,但这里有一些伪代码只是为了了解这个想法:

当用户在他的计算机上选择一个文件并按下提交按钮时,我的脚本被调用并且,......

  • 将文件上传到临时目录(我可以通过访问看到该图像http://localhost/drupal/sites/default/files/images/singe_6.jpg
  • 创建一个新的空 Fedora Commons 对象(我可以通过访问来查看该对象http://myFedoraServer:8082/fedora/objects/some%3Apid
  • 通过使用 cURL POST 到以下 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 错误响应,显示错误消息并退出

其他尝试

  • 将文件 ( ) 的路径更改为dsLocation相对路径 (/sites/default/files/images/singe_6.jpg而不是http://localhost/drupal/sites/default/files/images/singe_6.jpg)。没有任何改变。
  • 将文件的路径 ( ) 更改dsLocation为 Internet 上随机图片的绝对路径 (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");
    }
}

?>

结果

创建空对象成功。PID:人员:myObjectID

网址: http: //vitdevelapp-cen.cen.umontreal.ca :8082/fedora/objects/personne:myObjectID/datastreams/myDatastreamID?controlGroup=M&dsLocation=http://localhost/drupal/sites/default/files/images/ sing_6.jpg

用户错误:(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/网站/默认/文件/图像/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

4

1 回答 1

1

Fedora 日志中的 500 错误消息文本是什么?Fedora 可能会因为很多愚蠢的原因抛出错误,但是如果没有错误消息文本,就很难开始调试这个问题。

我的第一个猜测是,图像“singe_6.jpg”可能不存在,或者 Fedora 服务器无法访问。“localhost”上的图像路径是否与您的 Fedora 服务器 vitdevelapp-cen.cen.umontreal.ca 相同?

于 2013-03-06T18:38:32.533 回答