PHP 中的 Jena Fuseki SPARQL INSERT(EasyRDF 库)

Jena Fuseki SPARQL INSERT in PHP (EasyRDF lib)

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

Fatal error: Uncaught exception 'EasyRdf_Exception' with message 'HTTP request for http:// localhost:3030/test/update?graph=http%3A%2F%2Flocalhost%3A3030%2Ftest%2Ftime.rdf failed: Must be application/sparql-update or application/x-www-form-urlencoded (got application/n-triples)' in D:\Files\xampp\htdocs\test\easyrdf-0.9.0\lib\EasyRdf\GraphStore.php:152 Stack trace: #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} thrown in D:\Files\xampp\htdocs\test\easyrdf-0.9.0\lib\EasyRdf\GraphStore.php on line 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>

谢谢。

您混淆了 SPARQL 1.1 Protocol and the SPARQL 1.1 Graph Store HTTP Protocol
区别在于后者不使用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();

?>

来源:

有关详细信息,另请参阅