Search code examples
phprdffusekieasyrdfgraph-store-protocol

Jena Fuseki SPARQL INSERT in PHP (EasyRDF lib)


I'm trying to run the sample code from the EasyRDF library with Apache Jena Fuseki, but the following error occurs when entering data into the database:

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

Follow the code:

<?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>

Thanks.


Solution

  • You are mixing up the SPARQL 1.1 Protocol and the SPARQL 1.1 Graph Store HTTP Protocol.
    The difference is that the latter doesn't use SPARQL queries to perform operations on RDF graphs.

    For every protocol, Fuseki exposes two URIs: for read and for write operations.

    enter image description here

    Thus, if you want to use the SPARQL 1.1 Graph Store HTTP Protocol, you should write:

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

    instead of

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

    If you need to use the SPARQL 1.1 Protocol, write something like this:

    <?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();
    
    ?>
    

    Sources:

    See also this answer for some details.