Created
October 17, 2010 16:34
-
-
Save njh/631005 to your computer and use it in GitHub Desktop.
Parse RDF/XML to RDF/PHP using the redland PHP extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$URL = 'http://dbpedia.org/resource/London.rdf'; | |
$DATA = file_get_contents('london.rdf'); | |
$FORMAT = "rdfxml"; | |
if (!extension_loaded('redland')) { | |
throw new Exception("redland extension is not loaded"); | |
} | |
$librdfWorld = librdf_php_get_world(); | |
$start = microtime(true); | |
$librdfStorage = librdf_new_storage($librdfWorld, "memory", null, null); | |
if (!$librdfStorage) { | |
throw new Exception( | |
"Failed to create librdf_storage" | |
); | |
} | |
$librdfModel = librdf_new_model($librdfWorld, $librdfStorage, null); | |
if (!$librdfModel) { | |
throw new Exception( | |
"Failed to create librdf_model" | |
); | |
} | |
$librdfParser = librdf_new_parser($librdfWorld, $FORMAT, null, null); | |
if (!$librdfParser) { | |
throw new Exception( | |
"Failed to create librdf_parser of type: $FORMAT" | |
); | |
} | |
$librdfSerializer = librdf_new_serializer($librdfWorld, 'json', null, null); | |
if (!$librdfSerializer) { | |
throw new Exception( | |
"Failed to create librdf_serializer of type: 'json'" | |
); | |
} | |
$librdfUri = librdf_new_uri($librdfWorld, $URL); | |
if (!$librdfUri) { | |
throw new Exception( | |
"Failed to create librdf_uri from: $URL" | |
); | |
} | |
$result = librdf_parser_parse_string_into_model($librdfParser, $DATA, $librdfUri, $librdfModel); | |
if ($result) { | |
throw new Exception( | |
"Failed to parse '$URL' into model: $result" | |
); | |
} | |
$json = librdf_serializer_serialize_model_to_string($librdfSerializer, $librdfUri, $librdfModel); | |
$rdfphp = json_decode($json); | |
librdf_free_uri($librdfUri); | |
librdf_free_serializer($librdfSerializer); | |
librdf_free_parser($librdfParser); | |
librdf_free_model($librdfModel); | |
librdf_free_storage($librdfStorage); | |
$duration = microtime(true) - $start; | |
print " Parse time: $duration seconds\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@njh, thank you for your reply. Does the
redland
extension work well onphp-7
?It seems that many people use the
EasyRdf
package to processrdf
file inphp-7
.