PHP Example

We would like to make the usage of the API as easy as possible. Developers are (after the API registration) able to post events and to request context models. Everything that can be done with our API is described in detail in the API documentation. It is also described how requests can be sent. Since this can be at first to think of every detail in the implementation, we would like to offer a set of classes that can be easily integrated into a PHP project.

To integrate these classes into a PHP project, just copy the files in the same directory as the script that will do the requests (or you have to adjust the path to the classes)

require_once 'learning-context/LearningContext.php';

After this, the functionalities of these classes are available to your PHP script. You won't have to worry about what has to be sent to the interface or how it should be formatted or calculated. The given classes will take care of everything except for storing the token(s). In our example, this is done by saving the Refresh-Token into the session. Therefore, it has to be checked whether the there is already a token in the session:

if(isset($_SESSION["rt"])) {
	$rt = $_SESSION["rt"];
} else {
	$rt = '';

Since a PHP script is not event driven like Java, there won't be a Listener to react on the results of the server. This is just plain "sending something and waiting for the answer". To get started, you have to create an object of the type ContextData. It requires the URL and the version of the API, the username and password of one valid user, the app-ID and the app-secret (you should have received that after registering your app).

$lc = new LearningContext(

To send a GET request (e.g. receive a context model from the API), only one additional line of code is required:

echo $lc->get("events", "{\"model\":\"COMPLETE\"}");

The first parameter defines the name of the interface (in this case it is events). The second parameter is the request itself. It is a JSON string as described in the API documentation.

To send a POST request (send new events to the API), the event and its entities have to be created first. Here, we will create an event which indicates a new position at a given timestamp. As entities, any key-value pairs can be added to an event (see API Doc):

$event = new Event("START", "2015-10-15 12:12:12", "MOBILE", "PRIVATE", "POSITION");

$entity1 = new Entity("lng", "6.44");
$entity2 = new Entity("lat", "50.33");

To convert the event into its JSON representation, the class Event has its own function, called toJson(). Since the API requires you to send an Array of events, you can simply put [ ] around a comma separated set of events (each formatted as a JSON string). After that, the JSON string can be send analogous to the GET request:

$json = "[" . $event->toJson() . "]";

echo $lc->post("events", $json);