Search…
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.
1
<?php
2
$token=<api-key>;
3
$company_id = <company-id>;
4
$app_id = <application-id>;
5
$username = <message creator email>
6
$debug=true;
7
$base_url="http://server-v1.2mee.com";
Copied!

Default audience segment

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

Audience count

1
$response = getRequest(false, "/filter/count/".$filter_id, $token);
2
error_log("Filter Device Count =".$response->devices->deviceCount);
3
$filter_device_count = $response->devices->deviceCount;
4
Copied!
** For getRequest(), postRequest(),putRequest() refer to Appendix.
1
Send rich text Messagez
2
3
$playtime = (time())*1000; // playtime in milliseconds now
4
error_log("Play time is set to now =".$playtime);
5
//NOTIFICATION
6
$data = array();
7
$data['appId'] = $app_id;
8
$data['title'] = 'PHP Test Image';
9
$data['subtitle'] ='PHP Test Image';
10
$data['text'] = 'This is a very short text test Image.';
11
$data['fbTitle'] = 'Fb Title';
12
$data['fbSubtitle'] ='Gb Subtitle';
13
$data['fbText'] = 'Fb This is a very short text test Image.';
14
$data['expiry'] = '1440';
15
$data['playTime'] = $playtime;
16
$data['about'] = [];
17
$data['filterId'] = $filter_id;
18
$data['companyId'] = $company_id;
19
$data['type'] = 'TEXT';
20
$data['priority'] = 'MEDIUM';
21
$data['channel'] = 'PUSH';
22
$data['favourite'] = 0;
23
$data['action'] = "ok";
24
$response = postRequest(false, "/notification/create", $token, $data);
25
error_log("NotificationId =".print_r($response->id, true));
26
$notification_id = $response->id;
Copied!

Upload image to text message

1
$filename = curl_file_create('image.jpg');
2
$image_date = array();
3
$image_date['id'] = $notification_id;
4
$image_date['file'] = $filename;
5
$response = postImageRequest(false, "/notification/upload", $token, $image_date);
6
Copied!

Schedule notification

1
$response = postRequest(false, "/notification/schedule?id=".$notification_id, $token, "");
2
$image_url = $response->url;
3
error_log("Image url ".$image_url);
Copied!
You should receive a notification on the demo app on the scheduled time.

Create holopush message

Company Artist List

1
$res=getRequest(false,"/user/list/artist?company_id=".$response->companyId,$token);
2
$artist_email = $res[0]->email;
3
error_log("Artist id=".$artist_email);
Copied!
1
$playtime = (time())*1000; // playtime in milliseconds now
2
error_log("Play time is set to now =".$playtime);
3
//NOTIFICATION
4
$data = array();
5
$data['appId'] = $app_id;
6
$data['title'] = 'PHP Test Image';
7
$data['subtitle'] ='PHP Test Image';
8
$data['text'] = 'This is a very short text test Image.';
9
$data['fbTitle'] = 'Fb Title';
10
$data['fbSubtitle'] ='Gb Subtitle';
11
$data['fbText'] = 'Fb This is a very short text test Image.';
12
$data['expiry'] = '1440';
13
$data['playTime'] = $playtime;
14
$data['about'] = [];
15
$data['filterId'] = $filter_id;
16
$data['companyId'] = $company_id;
17
$data['type'] = 'HOLOPUSH';
18
$data['priority'] = 'MEDIUM';
19
$data['channel'] = 'PUSH';
20
$data['favourite'] = 0;
21
$data['action'] = "ok";
22
$response = postRequest(false, "/notification/create", $token, $data);
23
error_log("NotificationId =".print_r($response->id, true));
24
$notification_id = $response->id;
Copied!

Create hologram message

1
$tomorrow = date("Y-m-d", time() + 86400);
2
//Job
3
$jobData = array();
4
$jobData['title'] = 'Testing the app';
5
$jobData['description'] = 'Job to test the app';
6
$jobData['notificationId'] = $notification_id;
7
$jobData['jobType'] = 'HOLOPUSH';
8
$jobData['appId'] = $app_id;
9
$jobData['dueDate'] = $tomorrow;
10
$jobData['script'] = 'This is a test';
11
$jobData['artist'] = $artist_email;
12
$jobData['holoPushLevel'] = 'TORSO';
13
$jobData['favourite'] = null;
14
$jobData['artistDueDate'] = $tomorrow;
15
$response = postRequest(false,"/job/create",$token,$jobData);
16
error_log("JobId =".print_r($response->id, true));
17
$jobId = $response->id;
Copied!

Holopush update script

1
$updateJob = array();
2
$updateJob['id'] = $response->id;
3
$updateJob['title'] = $response->title;
4
$updateJob['description'] = $response->description;
5
$updateJob['holoPushLevel'] = $response->holoPushLevel;
6
$updateJob['dueDate'] = $response->dueDate;
7
$updateJob['artist'] = $artist_email;
8
$updateJob['creator'] = $username;
9
$updateJob['script'] = 'This is a regular test';
10
$updateJob['artistDueDate'] = $tomorrow;
11
$response=putRequest(true,"/job/update",$token,$updateJob);
12
error_log("JobId =".print_r($response->id, true));
13
$jobId = $response->id;
14
Copied!

Artist assign

1
$response=postRequest(false,"/job/executeJob?id=".$jobId,$token,"");
Copied!
The artist should receive a hologram record request on the creator app.

Appendix

Get access token

Get request

1
function getRequest($debug, $api, $token)
2
{
3
$base_url=$GLOBALS['base_url'];
4
$ch = curl_init();
5
curl_setopt($ch, CURLOPT_URL, $base_url.$api);
6
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: application/json",));
7
if ($debug) {
8
curl_setopt($ch, CURLOPT_VERBOSE, true);
9
}
10
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
11
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
12
$res = curl_exec($ch);
13
curl_close($ch);
14
$response = json_decode($res, false);
15
if ($debug) {
16
error_log("Response String".$res);
17
error_log("Response Array".print_r($response, true));
18
}
19
return $response;
20
}
Copied!

Post request

1
function postRequest($debug, $api, $token, $postdata)
2
{
3
$base_url=$GLOBALS['base_url'];
4
$ch = curl_init();
5
curl_setopt($ch, CURLOPT_URL, $base_url.$api);
6
//[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, */*
7
//debugMessage] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
8
9
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: application/json",));
10
if ($debug) {
11
error_log("Post json data = ".json_encode($postdata));
12
curl_setopt($ch, CURLOPT_VERBOSE, true);
13
}
14
curl_setopt($ch, CURLOPT_POST, true);
15
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
16
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata));
17
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
18
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
19
$res = curl_exec($ch);
20
curl_close($ch);
21
$response = json_decode($res, false);
22
if ($debug) {
23
error_log("Response String".$res);
24
error_log("Response Array".print_r($response, true));
25
}
26
return $response;
27
}
Copied!

Post image request

1
function postImageRequest($debug, $api, $token, $postdata)
2
{
3
$base_url=$GLOBALS['base_url'];
4
$ch = curl_init();
5
curl_setopt($ch, CURLOPT_URL, $base_url.$api);
6
//[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, */*
7
//debugMessage] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
8
9
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: multipart/form-data"));
10
if ($debug) {
11
error_log("Post json data = ".json_encode($postdata));
12
curl_setopt($ch, CURLOPT_VERBOSE, true);
13
}
14
curl_setopt($ch, CURLOPT_POST, true);
15
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
16
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
17
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
18
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
19
$res = curl_exec($ch);
20
curl_close($ch);
21
$response = json_decode($res, false);
22
if ($debug) {
23
error_log("Response String".$res);
24
error_log("Response Array".print_r($response, true));
25
}
26
return $response;
27
}
Copied!

Put request

1
function putRequest($debug, $api, $token, $putdata)
2
{
3
$base_url=$GLOBALS['base_url'];
4
$ch = curl_init();
5
curl_setopt($ch, CURLOPT_URL, $base_url.$api);
6
//[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, */*
7
//debugMessage] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
8
9
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Authorization: Bearer ".$token, "Content-Type: application/json",));
10
if ($debug) {
11
error_log("put json data = ".json_encode($putdata));
12
curl_setopt($ch, CURLOPT_VERBOSE, true);
13
}
14
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
15
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($putdata));
16
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
17
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
18
$res = curl_exec($ch);
19
curl_close($ch);
20
$response = json_decode($res, false);
21
if ($debug) {
22
error_log("Response String".$res);
23
error_log("Response Array".print_r($response, true));
24
}
25
return $response;
26
}
Copied!
Copy link