phprdfeasyrdf

Easy RDF library is not returning proper RDF values


I have an remote RDF file from where I want to fetch some data. It is on remote server and the example data is working 100% fine, but soon I parse my file it starts giving the error. I am using Easy RDF library.

Here is the example they are providing:

<?php
    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "EasyRdf.php";
?>
<html>
<head>
  <title>Basic FOAF example</title>
</head>
<body>

<?php
  $foaf = EasyRdf_Graph::newAndLoad('http://njh.me/foaf.rdf');
  $me = $foaf->primaryTopic();
?>

<p>
  My name is: <?= $me->get('foaf:name') ?>
</p>

</body>
</html>

Here is my example from where i am getting an error.

<?php
    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "EasyRdf.php";
?>
<html>
<head>
  <title>Basic FOAF example</title>
</head>
<body>

<?php
  $foaf = EasyRdf_Graph::newAndLoad('http://gutenberg.readingroo.ms/cache/generated/4500/pg4500.rdf');
  $me = $foaf->primaryTopic();
?>

<p>
  My name is: <?= $me->get('dcterms:title') ?>
</p>

</body>
</html>

Error i am getting:

My name is: 
Fatal error: Call to a member function get() on a non-object in D:\xampp\htdocs\giftcardbooks\easyrdf\examples\basic.php on line 31

My ultimate goal is to get values from the RDF file. using its nodes and child. please have a look and help me out from it.


Solution

  • The documentation is far from complete, and it is very hard to find anything relevant in this topic. All you can do for further understanding is digging into the code, or asking your question here: https://groups.google.com/forum/#!forum/easyrdf.

    The primaryTopic() approach will never work

    According to the Graph class, the primaryTopic() does this:

            return $this->get(
                $resource,
                'foaf:primaryTopic|^foaf:isPrimaryTopicOf',
                'resource'
            );
    

    Which means it is looking for a resource which is in some kind of relation with foaf:primaryTopic. Your get() call returns null, since your RDF does not contain a resource like that.

    Currently this works for me with your RDF

    <html>
    <head>
        <title>Basic FOAF example</title>
    </head>
    <body>
    
    <?php
    /** @var EasyRdf_Graph $graph */
    $graph = EasyRdf_Graph::newAndLoad('http://gutenberg.readingroo.ms/cache/generated/4500/pg4500.rdf');
    
    /** @var EasyRdf_Resource $book */
    $book = $graph->resource('http://gutenberg.readingroo.ms/cache/generated/4500/ebooks/4500'); //returns the ebook resource
    
    $title = $book->get('dcterms:title'); //returns a literal node
    ?>
    
    <p>
        My name is: <?php echo $title; //calls the __toString() of the literal node
    ?>
    </p>
    
    </body>
    </html>
    

    An alternative approach to get the $book:

    $book = current($graph->resources());
    

    Since the book is a root node, it will be the first resource in the graph...

    I don't think there is a way to get the book using pgterms:ebook

    It is because EasyRdf does not recognizes that as a type. I think that is because it is not defined anywhere as an rdfs:Class, it is just used, but that's just a guess, to make it sure you have to check the code. The dcterms:title is not a type either, but it can be used as a resource property...

    var_dump($graph->types($book))
    ->
    array(1) {
      [0]=>
      NULL
    }
    

    Parse as XML

    It is much easier to parse your RDF file with an XML parser:

    $dom = simplexml_load_file('http://gutenberg.readingroo.ms/cache/generated/4500/pg4500.rdf');
    $title = (string) current($dom->xpath('//rdf:RDF/pgterms:ebook/dcterms:title'));
    $name = (string) current($dom->xpath('//rdf:RDF/pgterms:ebook/dcterms:creator/pgterms:agent/pgterms:name'));
    echo $title;
    echo $name;
    

    I think there is an issue in the EasyRdf or in your RDF file, but I could not find a way to get the name using EasyRdf. The dcterms:creator had an rdf:type of pgterms:agent, but it had no other properties nor had the agent... I could not find out what's the problem. I checked the $graph->resources() and the value of the missing properties were under generated IDs which seemed to have no contact with the $ebook. So I guess the parsing went not so well. This can be an EasyRdf error, or this can be a problem with the RDF file itself. I'll ask about it in the discussion forum, maybe they provide an answer.