• Stars
    star
    266
  • Rank 151,102 (Top 4 %)
  • Language
    PHP
  • License
    BSD 3-Clause "New...
  • Created almost 13 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP implementation of a JSON-LD Processor and API

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

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

$context = (object)array(
  "name" => "http://schema.org/name",
  "homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
  "image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")
);

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

echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
/* Output:
{
  "@context": {...},
  "image": "http://manu.sporny.org/images/manu.png",
  "homepage": "http://manu.sporny.org/",
  "name": "Manu Sporny"
}
*/

// compact using URLs
jsonld_compact('http://example.org/doc', 'http://example.org/context');

// expand a document, removing its context
// see: http://json-ld.org/spec/latest/json-ld/#expanded-document-form
$expanded = jsonld_expand($compacted) {
echo json_encode($expanded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
/* Output:
{
  "http://schema.org/image": [{"@id": "http://manu.sporny.org/images/manu.png"}],
  "http://schema.org/name": [{"@value": "Manu Sporny"}],
  "http://schema.org/url": [{"@id": "http://manu.sporny.org/"}]
}
*/

// expand using URLs
jsonld_expand('http://example.org/doc');

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

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

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

// force HTTPS-only context loading:
// use built-in secure document loader
jsonld_set_document_loader('jsonld_default_secure_document_loader');

// set a default custom document loader
jsonld_set_document_loader('my_custom_doc_loader');

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

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

// a custom loader that uses a simplistic in-memory cache (no invalidation)
global $cache;
$cache = array();
function cache_load($url) {
  global $jsonld_default_load_document, $cache;
  if(isset($cache[$url])) {
    return $cache[$url];
  }
  // use default loader
  $doc = call_user_func($jsonld_default_load_document, $url);
  $cache[$url] = $doc;
  return $doc;
}

// use the cache loader for just this call, witout modifying the default one
$compacted = jsonld_compact($foo, 'http://schema.org', array(
  'documentLoader' => 'cache_load'));

Commercial Support

Commercial support for this library is available upon request from Digital Bazaar: [email protected]

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}

More Repositories

1

forge

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps
JavaScript
4,789
star
2

jsonld.js

A JSON-LD Processor and API implementation in JavaScript
JavaScript
1,629
star
3

pyld

JSON-LD processor written in Python
Python
581
star
4

vc

W3C Verifiable Credentials implementation in JavaScript
JavaScript
138
star
5

jsonld-signatures

An implementation of the Linked Data Signatures specification for JSON-LD. Works in the browser and Node.js.
JavaScript
132
star
6

bedrock

Bedrock: A core foundation for rich Web applications.
JavaScript
59
star
7

did-cli

A client for managing Decentralized Identifiers
JavaScript
44
star
8

authn.io

Credential Mediator Polyfill
Vue
43
star
9

monarch

The Modular Networking Architecture - high-performance libraries for creating REST-based, JSON Web Services
C++
43
star
10

did-io

Decentralized identifier management library for browser and node.js
JavaScript
40
star
11

jsonld-request

LIbrary to load JSON-LD from stdin, URLs, or files.
JavaScript
38
star
12

crypto-ld

JavaScript
31
star
13

jsonld-cli

JSON-LD command line interface tool
JavaScript
31
star
14

json-ld

A Context-based JSON Serialization for Linked Data
Perl
25
star
15

credential-handler-polyfill

Credential Handler API polyfill
JavaScript
24
star
16

zcap

Linked Data Capabilities reference implementation
JavaScript
23
star
17

did-method-key

A did-io driver for the DID "key" method
JavaScript
23
star
18

payswarm-wordpress

PaySwarm plugin for WordPress
CSS
18
star
19

payswarm.js

A PaySwarm client for node.js
JavaScript
18
star
20

qram

Cram arbitrarily large data into multiple streaming QR-codes
JavaScript
18
star
21

rdf-canonize

An implementation of the RDF Dataset Normalization Algorithm in JavaScript.
JavaScript
15
star
22

hashlink

JavaScript implementation of Cryptographic Hyperlinks specification.
JavaScript
13
star
23

minimal-cipher

Minimal encryption/decryption JWE library, secure algs only, browser-compatible.
JavaScript
13
star
24

cborld

A Javascript CBOR-LD processor for web browsers and Node.js apps.
JavaScript
13
star
25

p3

The PaySwarm Payment Processor (p3)
JavaScript
12
star
26

edv-client

An Encrypted Data Vault Client
JavaScript
11
star
27

encrypted-data-vaults

A privacy-respecting mechanism for storing, indexing, and retrieving encrypted data at a storage provider.
HTML
10
star
28

vc-revocation-list

Verifiable Credentials Revocation List 2020 JavaScript implementation
JavaScript
10
star
29

payswarm

The PaySwarm Project is creating a standard mechanism for purchasing and re-selling digital goods online.
Python
10
star
30

ed25519-signature-2020

Ed25519Signature2020 Linked Data Proof suite for use with jsonld-signatures.
JavaScript
9
star
31

payswarm-python

Python client library for PaySwarm
Python
9
star
32

equihash

Equihash Proof of Work for Node.js
C++
9
star
33

opencred-idp

Open Credentials Identity Provider and demo websites
PHP
8
star
34

http-signature-header

JavaScript
8
star
35

vc-demo

A demonstration of issuing and verifying Verifiable Credentials.
8
star
36

forge-dist

A native JavaScript implementation of TLS, cryptography primitives, and other webapp tools.
7
star
37

bitmunk

An open source, copyright-aware peer-to-peer client that enables browser-based buying/selling of music, movies and television by fans.
C++
7
star
38

cc-structured-data

Analyzes HTML content for RDFa, Microdata and Microformats
Java
6
star
39

credential-handler-demo

Credential Handler API demo
Vue
6
star
40

rdf-canonize-native

A native implementation of the RDF Dataset Normalization Algorithm for Node.js.
JavaScript
6
star
41

ezcap

An opinionated Authorization Capabilities client
JavaScript
6
star
42

webkms-client

A JavaScript Web Kms client library
JavaScript
6
star
43

le-store-redis

Redis certificate storage back-end for Node Let's Encrypt
JavaScript
5
star
44

bedrock-ledger-storage-mongodb

A storage subsystem for Bedrock ledger.
JavaScript
5
star
45

web-ledger-client

An implementation of a a Web Ledger client
JavaScript
5
star
46

ed25519-verification-key-2018

Javascript library for generating and working with Ed25519 key pairs, for use with crypto-ld.
JavaScript
5
star
47

jsonld-document-loader

JavaScript
5
star
48

web-payments.io

Website-side of the payments polyfill
JavaScript
5
star
49

webid-demo

WebID demonstration source code
JavaScript
5
star
50

jsonld-patch

JSON patch for JSON-LD
JavaScript
5
star
51

base58-universal

Encode/decode using "The Base58 Encoding Scheme".
JavaScript
4
star
52

payment-handler-polyfill

A polyfill for the Payment Handler API
JavaScript
4
star
53

opencred-verifier

Open Credentials Verifier JavaScript API
JavaScript
4
star
54

vpqr

Takes a Verifiable Presentation, compresses it via CBOR-LD, and turns it into a QR Code. For Node.js and browser.
JavaScript
4
star
55

bedrock-ledger-node

A Bedrock module that supports the creation and management of decentralized ledgers.
JavaScript
4
star
56

base64url-universal

Encode/decode "Base64url Encoding" format of JSON Web Signature (JWS) RFC7517.
JavaScript
4
star
57

did-context

DID Context
JavaScript
4
star
58

x25519-key-agreement-key-2019

An X25519 (Curve25519) DH key implementation to work with the crypto-ld LDKeyPair API
JavaScript
4
star
59

http-signature-zcap-invoke

A library for invoking Authorization Capabilities via HTTP signatures
JavaScript
3
star
60

web-request-mediator

A mediator for requests made by relying party Web Apps that are fulfilled by third party service provider Web apps
JavaScript
3
star
61

bedrock-edv-storage

Encrypted Data Vault Storage for Bedrock Apps
JavaScript
3
star
62

ed25519-signature-2018

A Javascript Ed25519 signature suite, for use with jsonld-signatures in the browser and server-side.
JavaScript
3
star
63

bitstring

A Bitstring module for universal JavaScript
JavaScript
3
star
64

bedrock-ledger-consensus-continuity

Web Ledger Continuity Consensus Protocol
JavaScript
3
star
65

security-document-loader

A JSON-LD documentLoader library pre-loaded with core commonly used contexts (suites, VC, DIDs).
JavaScript
3
star
66

monarch-benchmark

A suite of tools for benchmarking Monarch and Apache
C++
2
star
67

bedrock-angular-lazy-compile

Bedrock AngularJS Lazy Compile
JavaScript
2
star
68

bedrock-web-vc-store

A Javascript library for storing Verifiable Credentials for Bedrock web apps
JavaScript
2
star
69

payment-handler-demo

Payment Handler API polyfill demo
HTML
2
star
70

base58-spec

The Base58 Encoding Scheme
XSLT
2
star
71

bedrock-angular-form

Bedrock AngularJS Form support
JavaScript
2
star
72

web-request-rpc

JSON-RPC for Web Request Polyfills
JavaScript
2
star
73

bedrock-kms-http

HTTP APIs for Bedrock Key Management
JavaScript
2
star
74

eslint-config-digitalbazaar

JavaScript
2
star
75

canivc

Community compatibility dashboard for the Verifiable Credential ecosystem.
JavaScript
2
star
76

obv3-test-suite

Open Badges v3 Test Suite
JavaScript
2
star
77

bedrock-idp

Bedrock Identity Provider
JavaScript
2
star
78

payswarm-news-demo

Demonstration of a PaySwarm Token application for bloggers and journalists
JavaScript
2
star
79

bedrock-account-http

HTTP APIs for Bedrock User Accounts
JavaScript
2
star
80

ed25519-verification-key-2020

Javascript library for generating and working with Ed25519VerificationKey2020 key pairs, for use with crypto-ld.
JavaScript
2
star
81

did-ssh

2
star
82

bedrock-mongodb

Bedrock mongodb module
JavaScript
2
star
83

vc-js-cli

JavaScript
2
star
84

ecdsa-secp256k1-verification-key-2019

JavaScript
2
star
85

http-client

An opinionated, isomorphic HTTP client.
JavaScript
2
star
86

bedrock-vue

Vue frontend framework running on Bedrock
JavaScript
2
star
87

chapi-demo-wallet

Credential Handler API Demo Wallet
HTML
2
star
88

bedrock-tokenization

A Bedrock module for tokenizing identifiers and storing encrypted related data
JavaScript
2
star
89

webkms-switch

A JavaScript Web Kms switch library
JavaScript
2
star
90

bedrock-views

Bedrock website views module
JavaScript
1
star
91

bedrock-authn-token

Simple token-based authentication for Bedrock apps
JavaScript
1
star
92

d-langtag-ext

An extension to the language tag to support text direction.
XSLT
1
star
93

edv

Encrypted Data Vault
1
star
94

did-method-key-spec

HTML
1
star
95

bedrock-ledger-agent

Bedrock module that provides management of ledger agents
JavaScript
1
star
96

http-proofs

A new HTTP Header to express cryptographic proofs, such as proof-of-work, when accessing a resource.
XSLT
1
star
97

bedrock-web-pdf417

JavaScript
1
star
98

bedrock-mail

Bedrock mail
JavaScript
1
star
99

cit-vocab

Concealed ID Tokens
HTML
1
star
100

bedrock-ldn-receiver

Bedrock module for Linked Data Notification Receiver
JavaScript
1
star