Created
October 18, 2010 08:13
-
-
Save njh/631908 to your computer and use it in GitHub Desktop.
Parse RDF/XML to RDF/PHP using the rapper command line tool
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"; | |
| $start = microtime(true); | |
| // Open a pipe to the rapper command | |
| $descriptorspec = array( | |
| 0 => array("pipe", "r"), | |
| 1 => array("pipe", "w"), | |
| 2 => array("pipe", "w") | |
| ); | |
| $process = proc_open( | |
| escapeshellcmd('rapper'). | |
| " --quiet ". | |
| " --input " . escapeshellarg($FORMAT). | |
| " --output json ". | |
| " --ignore-errors ". | |
| " --input-uri " . escapeshellarg($URL). | |
| " --output-uri -". | |
| " - ", | |
| $descriptorspec, $pipes, '/tmp', null | |
| ); | |
| if (is_resource($process)) { | |
| // $pipes now looks like this: | |
| // 0 => writeable handle connected to child stdin | |
| // 1 => readable handle connected to child stdout | |
| // 2 => readable handle connected to child stderr | |
| fwrite($pipes[0], $DATA); | |
| fclose($pipes[0]); | |
| $json = stream_get_contents($pipes[1]); | |
| fclose($pipes[1]); | |
| $rdfphp = json_decode($json); | |
| $error = stream_get_contents($pipes[2]); | |
| fclose($pipes[2]); | |
| // It is important that you close any pipes before calling | |
| // proc_close in order to avoid a deadlock | |
| $returnValue = proc_close($process); | |
| if ($returnValue) { | |
| throw new EasyRdf_Exception( | |
| "Failed to parse RDF ($returnValue): ".$error | |
| ); | |
| } | |
| } else { | |
| throw new EasyRdf_Exception( | |
| "Failed to execute rapper command." | |
| ); | |
| } | |
| $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