PHP example

Sample PHP code showing how to use 2Mee APIs in PHP.

Please make sure to obtain the below values from UI as directed

1. API key which is required for API calls in Authorization: Bearer<API Key > format. 2. Application Id. 3.Company Id. and substitute these values for $token $company_id and $app_id.

<?php
$token=<api-key>;
$company_id = <company-id>;
$app_id = <application-id>;
$username = <message creator email>
$debug=true;
$base_url="https://exchange-prod-g.2mee.com";

Default audience segment

$response = getRequest(false, "/filter/list?type=CUSTOM&showHidden=false&page=0&app_id=".$app_id, $token);
error_log("Filter Id for DEFAULT FILTER =".$response->content[0]->id);
$filter_id = $response->content[0]->id;

Audience count

$response = getRequest(false, "/filter/count/".$filter_id, $token);
error_log("Filter Device Count =".$response->devices->deviceCount);
$filter_device_count = $response->devices->deviceCount;
   

** For getRequest(), postRequest(),putRequest() refer to Appendix.

Send rich text Messagez   

    $playtime = (time())*1000; // playtime in milliseconds now
     error_log("Play time is set to now =".$playtime);
    //NOTIFICATION
    $data = array();
    $data['appId'] = $app_id;
    $data['title'] = 'PHP Test Image';
    $data['subtitle'] ='PHP Test Image';
    $data['text'] = 'This is a very short text test Image.';
    $data['fbTitle'] = 'Fb Title';
    $data['fbSubtitle'] ='Gb Subtitle';
    $data['fbText'] = 'Fb This is a very short text test Image.';
    $data['expiry'] = '1440';
    $data['playTime'] = $playtime;
    $data['about'] = [];
    $data['filterId'] =  $filter_id;
    $data['companyId'] = $company_id;
    $data['type'] = 'TEXT';
    $data['priority'] = 'MEDIUM';
    $data['channel'] = 'PUSH';
    $data['favourite'] = 0;
    $data['action'] = "ok";
    $response = postRequest(false, "/notification/create", $token, $data);
    error_log("NotificationId =".print_r($response->id, true));
    $notification_id = $response->id;

Upload image to text message

    $filename = curl_file_create('image.jpg');
    $image_date = array();
    $image_date['id'] = $notification_id;
    $image_date['file'] = $filename;
    $response = postImageRequest(false, "/notification/upload", $token, $image_date);

Schedule notification

    $response = postRequest(false, "/notification/schedule?id=".$notification_id, $token, "");
    $image_url = $response->url;
    error_log("Image url ".$image_url);

You should receive a notification on the demo app on the scheduled time.

Create holopush message

Company Artist List

$res=getRequest(false,"/user/list/artist?company_id=".$response->companyId,$token);
$artist_email = $res[0]->email;
error_log("Artist id=".$artist_email);
   $playtime = (time())*1000; // playtime in milliseconds now
     error_log("Play time is set to now =".$playtime);
    //NOTIFICATION
    $data = array();
    $data['appId'] = $app_id;
    $data['title'] = 'PHP Test Image';
    $data['subtitle'] ='PHP Test Image';
    $data['text'] = 'This is a very short text test Image.';
    $data['fbTitle'] = 'Fb Title';
    $data['fbSubtitle'] ='Gb Subtitle';
    $data['fbText'] = 'Fb This is a very short text test Image.';
    $data['expiry'] = '1440';
    $data['playTime'] = $playtime;
    $data['about'] = [];
    $data['filterId'] =  $filter_id;
    $data['companyId'] = $company_id;
    $data['type'] = 'HOLOPUSH';
    $data['priority'] = 'MEDIUM';
    $data['channel'] = 'PUSH';
    $data['favourite'] = 0;
    $data['action'] = "ok";
    $response = postRequest(false, "/notification/create", $token, $data);
    error_log("NotificationId =".print_r($response->id, true));
    $notification_id = $response->id;

Create hologram message

$tomorrow = date("Y-m-d", time() + 86400);
 //Job
    $jobData = array();
    $jobData['title'] = 'Testing the app';
    $jobData['description'] = 'Job to test the app';
    $jobData['notificationId'] = $notification_id;    
    $jobData['jobType'] = 'HOLOPUSH';
    $jobData['appId'] = $app_id;
    $jobData['dueDate'] = $tomorrow;
    $jobData['script'] = 'This is a test';
    $jobData['artist'] = $artist_email;
    $jobData['holoPushLevel'] = 'TORSO';
    $jobData['favourite'] = null;
    $jobData['artistDueDate'] = $tomorrow;
    $response = postRequest(false,"/job/create",$token,$jobData);
    error_log("JobId =".print_r($response->id, true));
    $jobId = $response->id;

Holopush update script

    $updateJob = array();
    $updateJob['id'] = $response->id;
    $updateJob['title'] = $response->title;
    $updateJob['description'] = $response->description;
    $updateJob['holoPushLevel'] = $response->holoPushLevel;
    $updateJob['dueDate'] = $response->dueDate;
    $updateJob['artist'] = $artist_email;
    $updateJob['creator'] = $username;
    $updateJob['script'] = 'This is a regular test';
    $updateJob['artistDueDate'] = $tomorrow;
    $response=putRequest(true,"/job/update",$token,$updateJob);
    error_log("JobId =".print_r($response->id, true));
    $jobId = $response->id;

Artist assign

    $response=postRequest(false,"/job/executeJob?id=".$jobId,$token,""); 

The artist should receive a hologram record request on the creator app.

Appendix

Get access token

Get request

function getRequest($debug, $api, $token)
{
    $base_url=$GLOBALS['base_url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $base_url.$api);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: application/json",));
    if ($debug) {
        curl_setopt($ch, CURLOPT_VERBOSE, true); 
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($res, false);
    if ($debug) {
        error_log("Response String".$res);
        error_log("Response Array".print_r($response, true));
    }
    return $response;
}

Post request

function postRequest($debug, $api, $token, $postdata)
{
    $base_url=$GLOBALS['base_url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $base_url.$api);
    //[message] => application/x-www-form-urlencoded;charset=UTF-8 media type is not supported. Supported media types are application/hal+json, application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, application/json, application/*+json, */*
    //debugMessage] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: application/json",));
    if ($debug) {
        error_log("Post json data = ".json_encode($postdata));
        curl_setopt($ch, CURLOPT_VERBOSE, true); 
    }
    curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($res, false);
    if ($debug) {
        error_log("Response String".$res);
        error_log("Response Array".print_r($response, true));
    }
    return $response;
}

Post image request

function postImageRequest($debug, $api, $token, $postdata)
{
    $base_url=$GLOBALS['base_url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $base_url.$api);
    //[message] => application/x-www-form-urlencoded;charset=UTF-8 media type is not supported. Supported media types are application/hal+json, application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, application/json, application/*+json, */*
    //debugMessage] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: multipart/form-data"));
    if ($debug) {
        error_log("Post json data = ".json_encode($postdata));
        curl_setopt($ch, CURLOPT_VERBOSE, true); 
    }
    curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($res, false);
    if ($debug) {
        error_log("Response String".$res);
        error_log("Response Array".print_r($response, true));
    }
    return $response;
}

Put request

function putRequest($debug, $api, $token, $putdata)
{
    $base_url=$GLOBALS['base_url'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $base_url.$api);
    //[message] => application/x-www-form-urlencoded;charset=UTF-8 media type is not supported. Supported media types are application/hal+json, application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, application/json, application/*+json, */*
    //debugMessage] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: application/json",));
    if ($debug) {
        error_log("put json data = ".json_encode($putdata));
        curl_setopt($ch, CURLOPT_VERBOSE, true); 
    }
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($putdata));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($res, false);
    if ($debug) {
        error_log("Response String".$res);
        error_log("Response Array".print_r($response, true));
    }
    return $response;
}

Last updated