1

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.

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58

1 Answers1

0

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.

inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • here is the response please:: Warning: Missing argument 2 for EasyRdf_Graph::get(), called in D:\xampp\htdocs\giftcardbooks\easyrdf\examples\basic.php on line 31 and defined in D:\xampp\htdocs\giftcardbooks\easyrdf\lib\EasyRdf\Graph.php on line 568 – adfsadasdsad Jun 09 '14 at 09:55
  • Notice: Undefined variable: propertyPath in D:\xampp\htdocs\giftcardbooks\easyrdf\lib\EasyRdf\Graph.php on line 576 Fatal error: Uncaught exception 'InvalidArgumentException' with message '$propertyPath should be a string or EasyRdf_Resource and cannot be null' in D:\xampp\htdocs\giftcardbooks\easyrdf\lib\EasyRdf\Graph.php:577 Stack trace: #0 D:\xampp\htdocs\giftcardbooks\easyrdf\examples\basic.php(31): EasyRdf_Graph->get('pgterms:name') #1 {main} thrown in D:\xampp\htdocs\giftcardbooks\easyrdf\lib\EasyRdf\Graph.php on line 577 – adfsadasdsad Jun 09 '14 at 09:56
  • these are the responses.. i think we are getting closer but no sure why it is giving so much errors. once again thank you fro the help in getting through this error. – adfsadasdsad Jun 09 '14 at 09:56
  • Yepp, I check it now on my dev machine. – inf3rno Jun 09 '14 at 10:01
  • Thank you please. kindly please download the rdf file as well in order to get the clear picture. – adfsadasdsad Jun 09 '14 at 10:02
  • 1
    any solution please? i am sorry i am getting panic btw. :( – adfsadasdsad Jun 09 '14 at 10:17
  • Thank You very much please. God bless you for the help. – adfsadasdsad Jun 09 '14 at 10:25
  • Getting closer: `$graph->resource('pgterms:ebook');` returns an `EasyRdf_Resource` of the `pgterms:ebook`, which has the `dcterms:title` property. But no luck yet with getting the value of that property. – inf3rno Jun 09 '14 at 10:38
  • OMG may be we need to use `get->('dcterms:title')` for getting value. can you try this please. i mean something like above. – adfsadasdsad Jun 09 '14 at 10:45
  • Nope, somehow the resource just create an empty resource and does not ask it from the graph, so that is not the right way. :S – inf3rno Jun 09 '14 at 10:48
  • i tried using `resource('pgterms:ebook'); ?>

    My name is: get('dcterms:title') ?>

    ` but still no lucks..
    – adfsadasdsad Jun 09 '14 at 10:50
  • Yes, the `pgterms:ebook` is the IRI of the resource type, but not a resource IRI, that's why it is not working... – inf3rno Jun 09 '14 at 11:01
  • its start giving much pain now, as they are also not providing much support on this. – adfsadasdsad Jun 09 '14 at 11:04
  • In theory `$graph->allOfType('pgterms:ebook')` should work, but I guess it does not, because there is no ebook type defined in anywhere. I guess `dcterms:title` works, because it is a property, and we asking property instead of type. But that's just a guess... `$graph->type($book)` returns NULL btw. – inf3rno Jun 09 '14 at 11:22
  • thank you please. so how the code actually looks like? can you please share? – adfsadasdsad Jun 09 '14 at 11:25
  • i have used: `echo $me = $graph->resource('dcterms:title');` and it returns `http://purl.org/dc/terms/title` but we need value here. what do you say? – adfsadasdsad Jun 09 '14 at 11:27
  • even it is not correct return as we do not have anything like that in the rdf file. :) – adfsadasdsad Jun 09 '14 at 11:29
  • thank you very much please. thank you again for the time and great help. but can you please explain this? `$graph->resource('http://gutenberg.readingroo.ms/cache/generated/4500/ebooks/4500');` – adfsadasdsad Jun 09 '14 at 11:45
  • i mean `http://gutenberg.readingroo.ms/cache/generated/4500/ebooks/4500` doesnot exists as directory on mirror ftp. – adfsadasdsad Jun 09 '14 at 11:46
  • its not returning me the value for `Meredith, George` can you please look on it.? – adfsadasdsad Jun 09 '14 at 11:48
  • please find the author name as well. from ``. please – adfsadasdsad Jun 09 '14 at 11:52
  • Ofc. it does not exist, these URLs are just identifiers, nothing more... For example there is nothing under the namespace URLs either... – inf3rno Jun 09 '14 at 11:53
  • i see got your point Sir. thank you for the explanation please. – adfsadasdsad Jun 09 '14 at 11:54
  • our one problem is 100% resolve now. i must seriously appreciate your great efforts with getting me through this issue. we need only name of authors now. – adfsadasdsad Jun 09 '14 at 11:56
  • Check your RDF with an XML viewer: the `pgterms:name` is under the `pgterms:ebook/dcterms:creator/pgterms:agent/pgterms:name`... – inf3rno Jun 09 '14 at 11:57
  • you are saying 100% correct. so how can we get its value now? – adfsadasdsad Jun 09 '14 at 11:58
  • kindly help me getting author name as well, so further if i need to get any more value i will simply follow this specific rule. – adfsadasdsad Jun 09 '14 at 12:00
  • I am also thinking the same, how it can be possible the value is not coming using get(); – adfsadasdsad Jun 09 '14 at 12:02
  • i tried using `$title = $book->get('pgterms:ebook|dcterms:creator|pgterms:agent|pgterms:name'); //returns a literal node` and it returns `My name is: http://gutenberg.readingroo.ms/cache/generated/12415/2009/agents/4353`.. – adfsadasdsad Jun 09 '14 at 12:10
  • no lucks? :) btw its really incomplete sort of library i think. – adfsadasdsad Jun 09 '14 at 12:22
  • A serialized the graph as JSON-LD it shows, that the `dcterms:creator` has a type of `pgterms:agent`, and it has a property of `pgterms:name`. Its IRI is `http://gutenberg.readingroo.ms/cache/generated/4500/2009/agents/520`. – inf3rno Jun 09 '14 at 12:31
  • :) so we need to do something else. :) it really getting so pain now. :( – adfsadasdsad Jun 09 '14 at 12:42
  • According to the `resource->properties()` the `dcterms:creator` has only an `rdf:type` which is an agent. So I think the RDF is malformed, that's why it is not working... :S – inf3rno Jun 09 '14 at 12:49
  • ahhhhh i seee you meant to say there is some error in the RDF file?? like some typos regarding proper nodes and childs definations? – adfsadasdsad Jun 09 '14 at 12:51
  • It is much easier to use an XML parser like SimpleXML and xpath to get the values you need. – inf3rno Jun 09 '14 at 12:51
  • but do you think RDF file mean this RDF file will work with SimpleXML and xpath?? – adfsadasdsad Jun 09 '14 at 12:53
  • I don't know what exactly is the problem, but I cannot retrieve the `pgterms:name` by using the `$book` node. I checked it in a JSON-LD format (which I am more familiar with), and the result graph had about 30 nodes, not just a single one (book) I expected. – inf3rno Jun 09 '14 at 12:54
  • Ofc it will work, it is a valid XML... It is a valid RDF too, but it seems to have flaws by describing your content, or EayRdf has errors by parsing this RDF (but I think the later is unlike). – inf3rno Jun 09 '14 at 12:55
  • Ok ok got you on this. can you provide some code on this or help related to this issue so we can resolve it using SimpleXML.. – adfsadasdsad Jun 09 '14 at 12:58
  • I edited my post, and added a SimpleXML example, I can do nothing more about this. Good luck! – inf3rno Jun 09 '14 at 13:31
  • I added a topic with title "Problems by traversing RDF graph" here: https://groups.google.com/forum/#!forum/easyrdf . It is not readable yet, maybe it waits for moderation, or it is denied to send in a new topic, I am not sure... – inf3rno Jun 09 '14 at 13:44
  • Thank you very very much for you great help so far with me. thank to stackoverflow having brilliant programmers in the site. – adfsadasdsad Jun 09 '14 at 13:48
  • thank you for the example as well. i will give it a try. – adfsadasdsad Jun 09 '14 at 13:48