This article describes how to create scripts to work with Epom REST API.

The sample scripts below are written using PHP as the most widespread server-side programming language for web servers. In these samples we will create a new user profile with Sign-up API, log into an account using Login API, fetch an Analytics report via Analytics API, and make sample requests to Publisher and Advertiser API.

Sign Up API

To create a new Advertiser with the following credentials:

  • Username: api_advertiser
  • Password: advertiser_password
  • Email: api_advertiser@epom.com
  • Role: ADVERTISER
  • Phone number: 1234567890
  • First name: John
  • Last name: Doe
  • Website: www.epom.com
<?php
   /* REST keys taken from REST APIs Reference tab in control interface of Epom Server */
   $rest_publickey  = '81535ee0e7678a5f';
   $rest_privatekey = '2b1460c932d85e16';
   $rest_timestamp  = time()*1000;

   /* Data of user that should be registered through API */
   $user_name      = 'api_advertiser';
   $password       = 'advertiser_password';
   $email          = 'api_advertiser@epom.com';
   $aclRole        = '3';
   $phone          = '1234567890';
   $first_name     = 'John';
   $last_name      = 'Doe';
   $website_url    = 'www.epom.com';

   /* Hash for request authentication and validation */
   $hash = md5($user_name.$password.$email.$rest_privatekey.$rest_timestamp);

   /* REST API mount point for user registration */
    $rest_url = "https://platform.wingatemedia.com/rest-api/user-sign-up/$rest_publickey/$hash/$rest_timestamp.do";
    
   /* Data being posted */
    $post_data = array(
       'username'       => $user_name,
       'password'       => $password,
       'email'          => $email,
       'aclRole'        => $aclRole,
       'phone'          => $phone,
       'firstName'      => $first_name,
       'lastName'       => $last_name,
       'websiteUrl'     => $website_url       
   );
   
    /* Options of HTTP request; http key should be used when posting to https url */
   $options = array(
       'http' => array(
           'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
           'method'  => 'POST',
           'content' => http_build_query($post_data)
       )
   );
   
   /* Actual call to REST URL */
   $context  = stream_context_create($options);
   $result   = file_get_contents($rest_url, false, $context);

   echo "$result\n";
?>

Login API

The script below returns the URL which logs the user with username "John" and password "password" to his account.

<?php
	/*Login API URL*/
	$url = "https://n1.epom.com/rest-api/auth/token.do";

    /*Username and password of the user who logs in*/
	$post_data = array(
		'username' => "john",
		'password' => "password"
	);

    /*specifying curl options*/
	$options = array(
		CURLOPT_URL => $url,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_POST => true, // POST method is used
		CURLOPT_POSTFIELDS => http_build_query($post_data), //POST request body parameters
		CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded'), //This header is mandatory in case if parameters are passed in request body.
		CURLOPT_RETURNTRANSFER => true
	);

    /*connection initiation*/
	$curl = curl_init();

    /*Applying curl options to our curl instance*/
	curl_setopt_array($curl,$options);

    /*Executing the call*/
	$result=curl_exec($curl);

    /*Retrieving token from the response*/
    $obj = json_decode($result);
	$token = $obj->authToken;

    /*Making a URL which will log the user in on request*/
	$login_url = "https://n1.epom.com/rest-api/auth/$token/login.do";
	echo $login_url;
?>

Analytics API

The code below fetches the Analytics report for today for "test" user grouped by Placement and Site.

<?php

    /*Initiating username and password variables, generating current timestamp and hash*/
    $username = "test";
    $password = "test";
    $timestamp  = round(microtime(true) * 1000);

    /*Constructing API url which fetches data in HTML format*/
    $hash = md5(md5($password).$timestamp);
    $url="https://n1.epom.com/rest-api/analytics/HTML/$username/$hash/$timestamp.do";

    /* Specifying that the report should be done for yesterday, grouped by Advertiser and Site */
    $post_data = array(
    "groupBy" => "SITE, PLACEMENT",
    "range" => "TODAY",
    "displayIds" => true
    );

    /* Specifying curl options */
    $options = array(
    CURLOPT_URL => $url,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query($post_data), //POST request body parameters
    CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded'),
    CURLOPT_RETURNTRANSFER => true
    );

    /* Connection initiation */
    $curl = curl_init();

    /* Applying curl options to our curl instance */
    curl_setopt_array($curl,$options);

    /* Executing the call */
    $result=curl_exec($curl);
    echo $result;

    ?>

Advertiser and Publisher APIs Requests

This script makes a GET request in order to fetch all child Zones of the Site with site ID 329.

<?php
	define("DEBUG",false);

	/*Initiating username and password variables, generating current timestamp and hash*/
	$username = "john";
	$password = "doe";
	$timestamp  = round(microtime(true) * 1000);
	$hash = md5(md5($password).$timestamp);

	/*Specifying API call URL for site ID 329*/
	$url = "https://n1.epom.com/rest-api/sites/329/zones.do";

	/*Specifying request parameters*/
	$post_data = array(
		"hash" => $hash,
		"timestamp" => $timestamp,
		"username" => $username
	);

	/*Specifying curl options*/
	$options = array(
		CURLOPT_URL => $url."?".http_build_query($post_data),
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_RETURNTRANSFER => true
	);

	/*Connection initiation*/
	$curl = curl_init();
	/*Applying curl options to our curl instance*/
	curl_setopt_array($curl,$options);

	/*Executing the call*/
	$result=curl_exec($curl);
	echo $result;

	if(DEBUG){
		echo "\n\n";
		echo $url;
		print_r($post_data);
	}
?>

The following script makes a POST request in order to create a new Zone for an existing Site with ID 329.

<?php
	define("DEBUG",false);

	/*Initiating username and password variables, generating current timestamp and hash*/
	$username = "john";
	$password = "doe";
	$timestamp  = round(microtime(true) * 1000);
	$hash = md5(md5($password).$timestamp);

	/*Specifying API call URL*/
	$url = "https://n1.epom.com/rest-api/zones/update.do";

	/*Specifying request parameters*/
	$post_data = array(
		"hash" => $hash,
		"timestamp" => $timestamp,
		"username" => $username,
		"name" => "new_zone",
		"siteId" => "329"
	);

	/*Specifying curl options*/
	$options = array(
		CURLOPT_URL => $url,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_POST => true, // POST method is used
		CURLOPT_POSTFIELDS => http_build_query($post_data), //POST request body parameters
		CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded'), //This header is mandatory in case if parameters are passed in request body.
		CURLOPT_RETURNTRANSFER => true
	);

	/*Connection initiation*/
	$curl = curl_init();

	/*Applying curl options to our curl instance*/
	curl_setopt_array($curl,$options);

	/*Executing the call*/
	$result=curl_exec($curl);
	echo $result;
	
	if(DEBUG){
		echo "\n\n";
		echo $url;
		print_r($post_data);
	}
?>

The script below makes a PUT request to link the Banner with ID 1071 to the Placements with IDs 816 and 817.

<?php
	define("DEBUG",false);

	/*Initiating username and password variables, generating current timestamp and hash*/
	$username = "john";
	$password = "doe";
	$timestamp  = round(microtime(true) * 1000);
	$hash = md5(md5($password).$timestamp);

	/*Specifying API call URL*/
	$url = "https://n1.epom.com/rest-api/banners/1071/placements.do";

	/*Specifying request parameters*/
	$post_data = array(
		"hash" => $hash,
		"timestamp" => $timestamp,
		"username" => $username,
		"ids" => "816,817"
	);

	/*Specifying curl options*/
	$options = array(
		CURLOPT_URL => $url,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_CUSTOMREQUEST => "PUT", //PUT method is used
		CURLOPT_POSTFIELDS => http_build_query($post_data),//specifying request body parameters
		CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded'),//This header is mandatory in case if parameters are passed in request body.
		CURLOPT_RETURNTRANSFER => true
	);

	/*Connection initiation*/
	$curl = curl_init();

	/*Applying curl options to our curl instance*/
	curl_setopt_array($curl,$options);

	/*Executing the call*/	
	$result = curl_exec($curl);
	echo $result;
	
	if(DEBUG){
		echo "\n\n";
		echo $url;
		print_r($post_data);
	}
?>

The script below makes a DELETE request to remove the Targeting by ID on the Banner level.

<?php
	/*Initiating username and password variables, generating current timestamp and hash*/
	$username = "john";
	$password = "doe";
	$timestamp  = round(microtime(true) * 1000);
	$hash = md5(md5($password).$timestamp);

	/*Specifying API call URL*/
        $url = "https://n1.epom.com/rest-api/banner/$banner_id/targeting/$targeting_id/delete.do";

	/*Specifying request parameters*/
	$post_data = array(
	"hash"      => $hash,
	"timestamp" => $timestamp,
	"username"  => $username,
	'bannerId'  => $banner_id,
	'targetId' => $targeting_id,
	);

	/*Specifying curl options*/
	$options = array(
	CURLOPT_URL            => $url . '?' . http_build_query($post_data),
	CURLOPT_SSL_VERIFYPEER => FALSE,
	CURLOPT_CUSTOMREQUEST => 'DELETE',
	CURLOPT_POSTFIELDS     => http_build_query($post_data),
	CURLOPT_RETURNTRANSFER => TRUE
	);

	/*Connection initiation*/
	$curl = curl_init();

	/*Applying curl options to our curl instance*/
	curl_setopt_array($curl, $options);

	/*Executing the call*/
	$result = curl_exec($curl);

	if(DEBUG){
	echo "\n\n";
	echo $url;
	print_r($post_data);
	}
?>

File Uploads Handling

The request below creates a Banner with local file creative.

<?php
	define("DEBUG",false);
	
	$username = "john";
	$password = "doe";
	$timestamp  = round(microtime(true) * 1000);
	$hash = md5(md5($password).$timestamp);

	$url = "https://n1.epom.com/rest-api/banner/create.do";

	/*Getting the full path to your file*/
	$file_name_with_full_path = realpath('300x250.gif');

	/*Specifying MIME type of the file*/
	$filetype = "image/gif";

	$post_data = array(
	"hash" => $hash,
	"timestamp" => $timestamp,
	"username" => $username,
	"placementType" => "SITE_PLACEMENT",
        "campaignId" => "1261",
        "active" => "true",
        "name" => "test_banner_1",
        "weight" => "1",
        "bannerType" => "LOCAL_FILE",
        "imageBannerLink" => "http://epom.com",
        "imageFile" => "@$file_name_with_full_path;type=$filetype",
        "url" => "http://epom.com",
        "adUnitId" => "1"
	);
	
	$options = array(
		CURLOPT_URL => $url,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_POST => true,		
		CURLOPT_POSTFIELDS => $post_data, 
		CURLOPT_HTTPHEADER => array("Content-type: multipart/form-data"),		
		CURLOPT_RETURNTRANSFER => true
	);
	
	$curl = curl_init();
	
	curl_setopt_array($curl,$options);	
	echo curl_exec($curl);
	
	if(DEBUG){
		echo "\n\n";
		echo $url;
		print_r($post_data);
	}		
?>


Need more help?

Should you require any additional help with our solutions, do not hesitate to contact Epom technical support at support@epom.com. Our support managers are dedicated to resolving any issues and providing you with solutions and support if you face difficulties or have questions.