Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Basic LDN receiver. Help yourself. See:
Copyright 2017 Amy Guy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
Quick and dirty Linked Data Notifications receiver
This PHP script is a minimum viable LDN receiver, or Inbox. You can place this
file on a server, and advertise its presence on any Web resource with an HTTP header:
Link: <>; rel=""
or RDF link, eg. JSON-LD:
"@context": "",
"@id": "",
"inbox": ""
eg. RDFa:
<link href="" rel="" />
Notifications sent to this Inbox are stored without modification to a flat file. An
alternative would be to parse the contents as RDF and/or save them in a database. The
notifications are exposed for other client applications (LDN consumers) to discover
and reuse.
This script assumes it lives in the same directory as where your notification
files are stored.
Find out more about Linked Data Notifications:
- Walkthrough of this code:
- W3C Recommendation:
$base = "https://".$_SERVER['HTTP_HOST']; // Your domain
$inboxpath = "inbox"; // The directory where your notification files are stored.
header("Accept-Post: application/ld+json");
/* Accepting notifications */
$input = file_get_contents('php://input');
$headers = apache_request_headers();
$data = json_decode($input, true);
if(strpos($headers["Content-Type"], "application/ld+json") === false){
header("HTTP/1.1 415 Unsupported Media Type");
header("HTTP/1.1 400 Bad Request");
echo "Invalid payload.";
// Write notification contents to a file
$filename = $inboxpath."/".date("ymd-His")."_".uniqid().".json";
$data["@id"] = $base."/".$filename;
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$h = fopen("../".$filename, 'w');
fwrite($h, $json);
header("HTTP/1.1 201 Created");
header("Location: ".$base."/".$filename);
/* Serving notifications */
$files = scandir("../".$inboxpath);
$notifications = array();
foreach($files as $file){
if(!is_dir($file) && substr($file, -5) == ".json"){
$notifications[] = array("@id" => $base."/".$inboxpath."/".$file);
$inbox = array( "@context" => ""
,"@id" => ""
,"@type" => "ldp:Container"
,"ldp:contains" => $notifications
$inboxjson = json_encode($inbox, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
header("Content-Type: application/ld+json");
echo $inboxjson;

This comment has been minimized.

Copy link

@AAorris AAorris commented Oct 4, 2018


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.