如何使用PHP摄入Fedora Commons?

时间:2013-03-05 19:34:04

标签: php curl fedora-commons

我正在尝试使用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到以下网址,将文件作为数据流附加到空对象: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中创建一个上传文件表单并尝试将该文件保存到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:personne:myObjectID

     

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

     

用户错误:(0)curl_getinfo(...)返回了意外的http代码   (预计201,但得到500)。错误:。完整信息:数组([url] =&gt;   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] =&gt; [http_code] =&gt; 500 [header_size] =&gt; 215   [request_size] =&gt; 330 [filetime] =&gt; -1 [ssl_verify_result] =&gt; 0   [redirect_count] =&gt; 0 [total_time] =&gt; 0.602249 [namelookup_time] =&gt;   1.9E-5 [connect_time] =&gt; 0.005847 [pretransfer_time] =&gt; 0.005849 [size_upload] =&gt; 0 [size_download] =&gt; 0 [speed_download] =&gt; 0   [speed_upload] =&gt; 0 [download_content_length] =&gt; 0   [upload_content_length] =&gt; -1 [starttransfer_time] =&gt; 0.602222   [redirect_time] =&gt; 0 [certinfo] =&gt; 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

1 个答案:

答案 0 :(得分:1)

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

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