PHP SDK

Moonify is an innovative solution to help you monetize your traffic.

Introduction

The PHP SDK is basically an API REST wrapper, it makes it easier to use. This server-side SDK is complementary to client-side one.

Unlike the Javascript SDK, you can pass some private information to our servers using this server-side SDK.

The web server hosting your site will call out our server first to open a new monetizing session. During this call you can pass several pieces of information, such as the connected user ID, and other -private- information. Next it returns a token usable with the client-side SDK.

To start playing with the PHP SDK you will need:

Please refer to the Getting started section, if you don’t know what it is.

Important note : Never share your private key with any one, it is used to authenticate with the API.

Quick integration

This example starts a monetization session jointly with the JS SDK.

In all the documentation, we assume that $moonify is the instance of the Moonify class provided in the PHP SDK.

<?php
include("moonify.php"); //include the wrapper

$moonify = new Moonify(); //declare a new moonify instance

$moonify->set("serviceID","[YOUR PRIVATE KEY]"); //set your private ID as authentication

$session ​=​ $moonify->session([​'userID'​=>​'MyUserID'​])->open(); //open a monetization session

// First method :
if($moonify->error){
    echo $moonify->error; //show error
} else {
    echo $session->integrationCode; //add Javascript code to set the monetization on client-side
}

// Second method : 
if($moonify->error){
    echo $moonify->error; //show error
} else {
    $tokenID = $session->tokenID();
    echo '<script src="https://pkg.moonify.io/moonify.min.js"></script>';
    echo '<script type="text/javascript">Moonify.set({tokenID : "'.$tokenID.'" });</script>';
}
?>

Authentication

Almost all requests need to be authenticated using your private serviceID. You have to set your private serviceID before the first request with the “set” method as below. It’s not mandatory to open a monetizing session to perform other requests.

$moonify->set("serviceID","[YOUR PRIVATE KEY]"); //set your private ID as authentication

Handling errors

Our API raise errors for many reasons, after making a request, you should check if the API has returned an error.

Example

if($moonify->error){
    echo $moonify->error; //show error
} else {
    //there is no error, do something
}

Pagination

For all fetched lists of resources, we support pagination. To navigate across lists, you must use at least the ->start() method.

Example

$moonify->users()->start(100)->limit(100)->get(); //example 1
$moonify->users()->order("score")->start(200)->limit(100)->get();  //example 2
$moonify->group("team","blue")->users()->order("score")->limit(5)->start(5)->get(); //example 3

Methods & arguments

Method Param Default Description
start(start) start (int) 0 A cursor for use in pagination. This is the item id that defines where you start.
limit(limit) limit (int) 100 A limit on the number of objects to be returned, between 1 and 100.

Sorting

For some fetched lists of resources, we support different sorting methods. You have to use the ->order() method.

Example

$moonify->users()->order("score")->get(); //example 1
$moonify->group("team","blue")->users()->order("score","desc")->get(); //example 2

Methods & arguments

Method Param Default Description
order(column,direction) column (string) - A column name on which you want to sort
direction (string) desc A direction (‘asc’ = ascendant, ‘desc’ = descendant)

API Reference

Sessions

Open a session

Initialize a Moonify session (required to start monetization). You can pass in parameter an userID (optional) and multiples groups (optional). It results in a creation of an user and/or groups.

The user is always registered in the last declared groups, and the score is also incremented in theses groups.

//open a session with a the userID 'MyUserID' and register it in 2 groups, the team blue, and in the kind paladin.
$moonify->session([​'userID'​=>​'MyUserID','@team'=>'blue','@kind'=>'paladin'])->open(); 

//all parameters preceded with @ is recognized as a group.

Methods & arguments

Method Param Default Description
->session(array) userID (optional) - A column name on which you want to sort
@group - A group name in property, a group value in value
->open() - - -

Users

List of all users

Get list and details of all users of your service.

$moonify->users()->get();

Limitation : 100 users per request. You should use pagination to get next users. Sorting possibilities are limited to few indexes. (score/desc only at this time).

Note : All statistics returned by lists are updated each 5 minutes.

Methods & arguments

Method Param Default Description
->users() - - -
->get() - - -

Retrieve an user

Get the details of a given user

$moonify->users("userID")->get();

Methods & arguments

Method Param Default Description
->users(userID) userID - An existing declared userID
->get() - - -

Groups

Retrieve a group

Get the details of a given group

The score of a group represents the total score that each user has made in that group.

$moonify->group("team","blue")->get();

Methods & arguments

Method Param Default Description
->group(groupName, groupValue) groupName - An existing declared groupName
groupValue - An existing declared groupValue
->get() - - -

List of all users in a group

Get list and details of all users of a given group

The score of an user in group represents the total score that this user has made in this group.

Limitation : 100 users per request. You should use pagination to get next users. Sorting possibilities are limited to few indexes. (score/desc only at this time).

Note : All statistics returned by lists are updated each 5 minutes.

$moonify->group("team","blue")->users()->get();

Methods & arguments

Method Param Default Description
->group(groupName, groupValue) groupName - An existing declared groupName
groupValue - An existing declared groupValue
->users() - - -
->get() - - -

Retrieve a user in a group

Get the details of a given user in a given group

The score of an user in group represents the total score that this user has made in this group.

$moonify->group("team","blue")->users("userID")->get();

Methods & arguments

Method Param Default Description
->group(groupName, groupValue) groupName - An existing declared groupName
groupValue - An existing declared groupValue
->users(userID) userID - An existing declared userID
->get() - - -