【问题标题】:Jena Fuseki SPARQL INSERT in PHP (EasyRDF lib)PHP 中的 Jena Fuseki SPARQL INSERT (EasyRDF lib)
【发布时间】:2023-04-07 03:37:01
【问题描述】:

我正在尝试使用 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 (得到 application/n-triples)' in 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>

谢谢。

【问题讨论】:

    标签: php rdf fuseki easyrdf graph-store-protocol


    【解决方案1】:

    您混淆了SPARQL 1.1 ProtocolSPARQL 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();
    
    ?>
    

    来源:

    有关详细信息,另请参阅this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      相关资源
      最近更新 更多