digitalbazaar/php-json-ld

Name: php-json-ld

Owner: Digital Bazaar, Inc.

Description: PHP implementation of a JSON-LD Processor and API

Created: 2011-08-01 18:49:00.0

Updated: 2018-01-10 18:39:51.0

Pushed: 2016-04-25 04:18:18.0

Homepage: https://github.com/digitalbazaar/php-json-ld

Size: 905

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

php-json-ld

Build Status

Introduction

This library is an implementation of the JSON-LD specification in PHP.

JSON, as specified in RFC7159, is a simple language for representing objects on the Web. Linked Data is a way of describing content across different documents or Web sites. Web resources are described using IRIs, and typically are dereferencable entities that may be used to find more information, creating a “Web of Knowledge”. JSON-LD is intended to be a simple publishing method for expressing not only Linked Data in JSON, but for adding semantics to existing JSON.

JSON-LD is designed as a light-weight syntax that can be used to express Linked Data. It is primarily intended to be a way to express Linked Data in JavaScript and other Web-based programming environments. It is also useful when building interoperable Web Services and when storing Linked Data in JSON-based document storage engines. It is practical and designed to be as simple as possible, utilizing the large number of JSON parsers and existing code that is in use today. It is designed to be able to express key-value pairs, RDF data, RDFa data, Microformats data, and Microdata. That is, it supports every major Web-based structured data model in use today.

The syntax does not require many applications to change their JSON, but easily add meaning by adding context in a way that is either in-band or out-of-band. The syntax is designed to not disturb already deployed systems running on JSON, but provide a smooth migration path from JSON to JSON with added semantics. Finally, the format is intended to be fast to parse, fast to generate, stream-based and document-based processing compatible, and require a very small memory footprint in order to operate.

Quick Examples
 = (object)array(
ttp://schema.org/name" => "Manu Sporny",
ttp://schema.org/url" => (object)array("@id" => "http://manu.sporny.org/"),
ttp://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png")


text = (object)array(
ame" => "http://schema.org/name",
omepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
mage" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")


ompact a document according to a particular context
ee: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
pacted = jsonld_compact($doc, $context);

 json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
utput:

context": {...},
mage": "http://manu.sporny.org/images/manu.png",
omepage": "http://manu.sporny.org/",
ame": "Manu Sporny"



ompact using URLs
ld_compact('http://example.org/doc', 'http://example.org/context');

xpand a document, removing its context
ee: http://json-ld.org/spec/latest/json-ld/#expanded-document-form
anded = jsonld_expand($compacted) {
 json_encode($expanded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
utput:

ttp://schema.org/image": [{"@id": "http://manu.sporny.org/images/manu.png"}],
ttp://schema.org/name": [{"@value": "Manu Sporny"}],
ttp://schema.org/url": [{"@id": "http://manu.sporny.org/"}]



xpand using URLs
ld_expand('http://example.org/doc');

latten a document
ee: http://json-ld.org/spec/latest/json-ld/#flattened-document-form
ttened = jsonld_flatten($doc);
ll deep-level trees flattened to the top-level

rame a document
ee: http://json-ld.org/spec/latest/json-ld-framing/#introduction
med = jsonld_frame($doc, $frame);
ocument transformed into a particular tree structure per the given frame

ormalize a document using the RDF Dataset Normalization Algorithm
URDNA2015), see: http://json-ld.github.io/normalization/spec/
malized = jsonld_normalize(
oc, array('algorithm' => 'URDNA2015', 'format' => 'application/nquads'));
ormalized is a string that is a canonical representation of the document
hat can be used for hashing, comparison, etc.

orce HTTPS-only context loading:
se built-in secure document loader
ld_set_document_loader('jsonld_default_secure_document_loader');

et a default custom document loader
ld_set_document_loader('my_custom_doc_loader');

 custom loader that demonstrates using a simple in-memory mock for
ertain contexts before falling back to the default loader
ote: if you want to set this loader as the new default, you'll need to
tore the previous default in another variable first and access that inside
he loader
al $mocks;
ks = array('http://example.com/mycontext' => (object)array(
ombre' => 'http://schema.org/name'));
tion mock_load($url) {
obal $jsonld_default_load_document, $mocks;
(isset($mocks[$url])) {
// return a "RemoteDocument", it has these three properties:
return (object)array(
  'contextUrl' => null,
  'document' => $mocks[$url],
  'documentUrl' => $url);

 use default loader
turn call_user_func($jsonld_default_load_document, $url);


se the mock loader for just this call, witout modifying the default one
pacted = jsonld_compact($foo, 'http://example.com/mycontext', array(
ocumentLoader' => 'mock_load'));

 custom loader that uses a simplistic in-memory cache (no invalidation)
al $cache;
he = array();
tion cache_load($url) {
obal $jsonld_default_load_document, $cache;
(isset($cache[$url])) {
return $cache[$url];

 use default loader
oc = call_user_func($jsonld_default_load_document, $url);
ache[$url] = $doc;
turn $doc;


se the cache loader for just this call, witout modifying the default one
pacted = jsonld_compact($foo, 'http://schema.org', array(
ocumentLoader' => 'cache_load'));
Commercial Support

Commercial support for this library is available upon request from Digital Bazaar: support@digitalbazaar.com

Source

The source code for the PHP implementation of the JSON-LD API is available at:

http://github.com/digitalbazaar/php-json-ld

Tests

This library includes a sample testing utility which may be used to verify that changes to the processor maintain the correct output.

To run the sample tests you will need to get the test suite files by cloning the json-ld.org and normalization repositories hosted on GitHub:

Then run the PHPUnit test.php application and point it at the directories containing the tests:

phpunit --group json-ld.org test.php -d {PATH_TO_JSON_LD_ORG/test-suite}
phpunit --group normalization test.php -d {PATH_TO_NORMALIZATION/tests}

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.