|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Example of grabbing and parsing Linked Data from DBPedia. |
| 4 | + * |
| 5 | + * This example illustrates how QueryPath can be used to do the following: |
| 6 | + * |
| 7 | + * - Make a robust HTTP connection to a remote server to fetch data. |
| 8 | + * - Using context to control the underlying stream. |
| 9 | + * - Working with Linked Data. |
| 10 | + * - Work with XML Namespaces in documents. |
| 11 | + * * Using namespaces to access elements in selectors |
| 12 | + * * Using namespaces to access attributes in selectors |
| 13 | + * * Using namespaces to access attributes in XML methods. |
| 14 | + * |
| 15 | + * The code here connects to the DBPedia server and looks up the Linked |
| 16 | + * Data stored there for a particular Wikipedia entry (any Wikipedia |
| 17 | + * wiki name should work here). |
| 18 | + * |
| 19 | + * |
| 20 | + * @author M Butcher <[email protected]> |
| 21 | + * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. |
| 22 | + * @see http://www.w3.org/DesignIssues/LinkedData.html |
| 23 | + * @see http://dbpedia.org |
| 24 | + * @see sparql.php |
| 25 | + * @see musicbrainz.php |
| 26 | + */ |
| 27 | + |
| 28 | +require_once __DIR__ . '/../../vendor/autoload.php'; |
| 29 | + |
| 30 | +// The URL to look up: |
| 31 | +$url = 'https://dbpedia.org/resource/Ben_Sealey'; |
| 32 | + |
| 33 | +// HTTP headers: |
| 34 | +$headers = [ |
| 35 | + 'Accept: application/rdf,application/rdf+xml;q=0.9,*/*;q=0.8', |
| 36 | + 'Accept-Language: en-us,en', |
| 37 | + 'Accept-Charset: ISO-8859-1,utf-8', |
| 38 | + 'User-Agent: QueryPath/1.2', |
| 39 | +]; |
| 40 | + |
| 41 | +// The context options: |
| 42 | +$options = [ |
| 43 | + 'http' => [ |
| 44 | + 'method' => 'GET', |
| 45 | + 'protocol_version' => 1.1, |
| 46 | + 'header' => implode("\r\n", $headers), |
| 47 | + ], |
| 48 | +]; |
| 49 | + |
| 50 | +// Create a stream context that will tell QueryPath how to load the file. |
| 51 | +$context = stream_context_create($options); |
| 52 | + |
| 53 | +// Fetch the URL and select all rdf:Description elements. |
| 54 | +// (Note that | is the CSS 3 equiv of colons for namespacing.) |
| 55 | +// To add the context, we pass it in as an option to QueryPath. |
| 56 | +$qp = qp($url, 'rdf|Description', ['context' => $context]); |
| 57 | + |
| 58 | +printf('There are %d descriptions in this record.<br>' . PHP_EOL, $qp->count()); |
| 59 | + |
| 60 | +// Here, we use foaf|* to select all elements in the FOAF namespace. |
| 61 | +printf('There are %d DBO items in this record.<br><br>' . PHP_EOL, $qp->top()->find('dbo|*')->count()); |
| 62 | + |
| 63 | +// Standard pseudo-classes that are not HTML specific can be used on namespaced elements, too. |
| 64 | +echo 'About (RDFS): ' . $qp->top()->find('rdfs|label:first-of-type')->text() . '<br>' . PHP_EOL; |
| 65 | +echo 'About (FOAF): ' . $qp->top()->find('foaf|name:first-of-type')->text() . '<br>' . PHP_EOL; |
| 66 | + |
| 67 | +// Namespaced attributes can be retrieved using the same sort of delimiting. |
| 68 | +echo PHP_EOL . '<br>Comment:<br>' . PHP_EOL; |
| 69 | +echo $qp->top()->find('rdfs|comment[xml|lang="en"]')->text(); |
| 70 | +echo '<br>' . PHP_EOL; |
| 71 | + |
| 72 | +$qp->top(); |
| 73 | + |
| 74 | +echo PHP_EOL . '<br>Other Sites:<br>' . PHP_EOL; |
| 75 | +foreach ($qp as $item) { |
| 76 | + echo $item->attr('rdf:about') . '<br>' . PHP_EOL; |
| 77 | +} |
0 commit comments