Installation:
composer require sweetrdf/easyrdf
Ensure PHP 8.0+ and extensions (dom, mbstring, xmlreader) are enabled.
First Use Case: Load and query an RDF file (e.g., FOAF profile):
use EasyRdf\Graph;
$graph = new Graph('https://example.com/foaf.rdf');
$graph->load(); // Loads RDF from URL or file
$primaryTopic = $graph->primaryTopic();
echo $primaryTopic->get('foaf:name'); // Output: "John Doe"
Key Classes to Explore:
EasyRdf\Graph: Core class for RDF graphs.EasyRdf\Resource: Represents RDF resources.EasyRdf\Sparql\Client: Execute SPARQL queries.EasyRdf\Format: Handle RDF serialization/deserialization.From URLs/Files:
$graph = new Graph('http://example.com/data.rdf');
$graph->load(); // Auto-detects format (RDF/XML, Turtle, etc.)
From Strings:
$graph = new Graph();
$graph->parse($rdfString, 'application/rdf+xml');
Supported Formats:
$name = $resource->get('foaf:name'); // Returns literal value
$type = $resource->getType(); // Returns URI of the resource type
$sparql = new \EasyRdf\Sparql\Client('http://dbpedia.org/sparql');
$results = $sparql->query('SELECT ?name WHERE { ?person foaf:name ?name }');
foreach ($results as $result) {
echo $result->name;
}
$graph->addLiteral('http://example.org/subject', 'http://xmlns.com/foaf/0.1/name', 'Alice');
$graph->addResource('http://example.org/subject', 'http://xmlns.com/foaf/0.1/knows', 'http://example.org/other');
$serialized = $graph->serialise('application/n-triples');
file_put_contents('output.nt', $serialized);
foaf:Person:
$graph->setTypeMapper('foaf:Person', 'App\\Models\\FoafPerson');
$person = $graph->getResource('http://example.org/person');
// $person is now an instance of App\Models\FoafPerson
$store = new \EasyRdf\GraphStore('http://fuseki.example.org/');
$store->save($graph, 'http://example.org/graph1');
$retrievedGraph = $store->load('http://example.org/graph1');
$graphviz = $graph->getGraphviz();
file_put_contents('graph.dot', $graphviz);
$html = file_get_contents('https://example.com');
$graph = new \EasyRdf\Graph();
$graph->parse($html, 'text/html', ['extractOpenGraph' => true]);
echo $graph->get('og:title');
SPARQL CONSTRUCT/DESCRIBE Queries:
SparqlResult instead of Graph. Use EasyRdf\Sparql\Client with updated headers (see #72).$client = new \EasyRdf\Sparql\Client('http://endpoint');
$client->setAcceptHeader('application/rdf+xml'); // Force desired format
$graph = $client->query('CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }');
Turtle Parser Edge Cases:
true, false) require explicit typing:
$graph->addLiteral('http://example.org/subject', 'http://example.org/pred', 'true', 'http://www.w3.org/2001/XMLSchema#boolean');
RDF/XML Parsing:
xml:lang and datatype may break parsing (fixed in 1.18.2).libxml for complex XML:
libxml_use_internal_errors(true);
$graph->parse($xmlString, 'application/rdf+xml');
HTTP Client Dependencies:
zendframework/http was removed in 1.18.0. Use Laminas\Http\Client or Guzzle for custom HTTP logic.Label Generation:
rdfs:label, foaf:name) may not cover all cases. Use custom lists:
$label = $resource->label(null, ['foaf:name', 'schema:title']);
PHP 8+ Deprecations:
foreach on non-traversable objects (e.g., SparqlResult). Use:
foreach ($result as $binding) { ... } // Correct
foreach ($result->bindings as $binding) { ... } // Also works
echo $graph->dump(); // Human-readable output
$uri = new \EasyRdf\Uri('http://example.org');
if (!$uri->isValid()) { /* Handle error */ }
EasyRdf\Http\Client:
$client = new \EasyRdf\Http\Client();
$client->setDebug(true);
$cache = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
$graph = $cache->get('graph_key', function() use ($url) {
$graph = new Graph($url);
$graph->load();
return $graph;
});
N-Triples) or chunked SPARQL queries.Custom Parsers/Serializers:
Extend EasyRdf\Parser or EasyRdf\Serializer for new formats.
Example:
class CustomParser extends \EasyRdf\Parser {
public function parse($data, $format) { ... }
}
$graph->registerParser('application/custom', new CustomParser());
Type Mappers:
Implement EasyRdf\TypeMapper\MapperInterface for custom object mapping:
class CustomMapper implements \EasyRdf\TypeMapper\MapperInterface {
public function map($resource, $type) { ... }
}
$graph->setTypeMapper('http://example.org/Type', new CustomMapper());
HTTP Client Overrides: Replace the default client (e.g., for authentication):
$graph->setHttpClient(new \GuzzleHttp\Client());
$graph->setNamespace('foaf', 'http://xmlns.com/foaf/0.
How can I help you explore Laravel packages today?