2

我正在尝试使用 Apache Jena Fuseki 运行 EasyRDF 库中的示例代码,但是在将数据输入数据库时​​出现以下错误:

致命错误:未捕获的异常 'EasyRdf_Exception' 带有消息 'HTTP 请求 http:// localhost:3030/test/update?graph=http%3A%2F%2Flocalhost%3A3030%2Ftest%2Ftime.rdf 失败:必须是 application/sparql -update 或 application/x-www-form-urlencoded (got application/n-triples)' 在 D:\Files\xampp\htdocs\test\easyrdf-0.9.0\lib\EasyRdf\GraphStore.php:152 堆栈跟踪: #0 D:\Files\xampp\htdocs\test\easyrdf-0.9.0\lib\EasyRdf\GraphStore.php(217): EasyRdf_GraphStore->sendGraph('POST', Object(EasyRdf_Graph), 'time.rdf' , 'ntriples') #1 D:\Files\xampp\htdocs\test\graphstore.php(34): EasyRdf_GraphStore->insert(Object(EasyRdf_Graph), 'time.rdf') #2 {main} 在 D 中抛出: \Files\xampp\htdocs\test\easyrdf-0.9.0\lib\EasyRdf\GraphStore.php 在第 152 行

按照代码:

<?php
    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "easyrdf-0.9.0/lib/EasyRdf.php";
?>
<html>
<head>
  <title>GraphStore example</title>
</head>
<body>

<?php
  // Use a local SPARQL 1.1 Graph Store (eg RedStore)
  $gs = new EasyRdf_GraphStore('http://localhost:3030/test/update');

  // Add the current time in a graph
  $graph1 = new EasyRdf_Graph();
  $graph1->add('http://example.com/test', 'rdfs:label', 'Test');
  $graph1->add('http://example.com/test', 'dc:date', time());
  $gs->insert($graph1, 'time.rdf');

  // Get the graph back out of the graph store and display it
  $graph2 = $gs->get('time.rdf');
  print $graph2->dump();
?>

</body>
</html>

谢谢。

4

1 回答 1

1

您正在混淆SPARQL 1.1 协议SPARQL 1.1 图形存储 HTTP 协议
不同之处在于后者不使用 SPARQL 查询对 RDF 图执行操作。

对于每个协议,Fuseki 都公开了两个 URI:用于读取和写入操作。

在此处输入图像描述

因此,如果您想使用SPARQL 1.1 Graph Store HTTP 协议,您应该编写:

$gs = new EasyRdf_GraphStore('http://localhost:3030/test/data');

代替

$gs = new EasyRdf_GraphStore('http://localhost:3030/test/update');

如果您需要使用SPARQL 1.1 协议,请编写如下内容:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'easyrdf-0.9.0/lib/');
require_once "EasyRdf.php";

$endpoint = new EasyRdf_Sparql_Client('http://localhost:3030/test/query',
                                      'http://localhost:3030/test/update');

function insert_data() {
    global $endpoint;
    $result = $endpoint->update("
        PREFIX : <http://example.org/> 
        INSERT DATA {:alice :knows :bob. :alice :name 'alice'. :bob :name 'bob'}"
    );
}
function insert_where() {
    global $endpoint;
    $result = $endpoint->update ("
        PREFIX : <http://example.org/> 
        INSERT {?s :loves ?o}
        WHERE {?s :name 'bob'. ?o :name 'alice'}"
    );
}
function select_where() {
    global $endpoint;
    $result = $endpoint->query("
        SELECT * WHERE {?s ?p ?o}"
    );
    print ($result->numRows()); 
}

insert_data();   select_where();
insert_where();  select_where();

?>

资料来源:

另请参阅此答案以获取一些详细信息。

于 2018-04-21T19:55:40.677 回答