Using RDF in PHP - from Turtle to JSON-LD
PHP is a major programming language here at CloudObjects. And RDF is the format used for describing data stored in CloudObjects Core. These two reasons are more than enough to look into how to handle RDF data in PHP. Here’s how we do it.
As there was no suitable “one-fits-all” solution available at the time of development, two external libraries are used inside CloudObjects’ PHP-based components. One is Semsol’s ARC2 RDF library, and the other is Markus Lanthaler’s JSON-LD Processor for PHP. These two libraries cover different areas of our RDF usage.
ARC2 is a very comprehensive library that supports parsing and serializing all common traditional RDF serialization formats such as RDF/XML, N-Triples and Turtle (as well as the older RDF/JSON, but please forgot about this, it will only confuse you …). While supporting all these for input and output, RDF data is represented internally as very lightweight PHP arrays. Actually there are two types of these internal structures, the flat listing of triples and the hierarchical index format. These internal formats make it possible to work on RDF data using simple PHP array functions. ARC2 is considered feature-complete and no longer actively developed. There is only very limited experimental JSON-LD support, thus another library is necessary.
The JSON-LD processor library supports the JSON-LD API for converting between the different JSON-LD formats (expanded, compacted, framed). It also supports convertion from and to RDF quads. Quads are actually just triples with one additional optional piece of information, the graph to which the triple belongs; for scenarios where this information is not required quads and triples can be used interchangeably. The library presents quads as an array of quad objects. Finally the library has an object-oriented interface to represent RDF data as PHP objects.
While developing CloudObjects Core and other components we’ve realized that it can actually be quite useful to mix and match these two libraries and turn the need for two libraries into an advantage. Sometimes, for example, one might get data in a format supported by ARC2, such as RDF/XML, but processing would be easiest with the object-oriented interface from the JSON-LD library. Or vice versa, there’s data in JSON-LD that one wants to work on using the simple array structures from ARC2.
After writing some conversion code inside different components we’ve extracted these pieces into a separate class and started an open source project under which helpers for working with RDF data can be collected in a single place. It’s a small library simply called RDF Utilities. It’s available via packagist, the source is on GitHub and documentation is published on rdfutilities-docs.cloudobjects.io (using the same process that we use for our PHP SDK).
So far the library has two classes. One is
CloudObjects\Utilities\RDF\Arc2JsonLdConverter, which has static conversion methods such as
quadsToIndex(). Have a look at the documentation for this class to see all methods. The other class is
CloudObjects\Utilities\RDF\Parser, which automatically determines the right library to parse a string of RDF data. See its documention as well.
This library is an ongoing project. We hope it’s useful and we’d love to see feedback and pull requests with enhancements and bugfixes!