Follow

Server Side Script Type Install Codes

Install Code

Contents

[hide]

Overview

Publisher Install Code is required to integrate TapIt ads into your mobile web site. Select the scripting language for your mobile we site below and copy/paste the integration code into your page(s) where you want the ad to appear. Replace the "PLACE YOUR ZONE ID HERE" text with Zone ID of your zone/ad unit. Zone ID's are provided by your account manager or can be generated through TapIt's Self-Service Platform. Once the integration is validated by receiving the TapIt Blue Test Banner, change the mode to "live" and start receiving live mobile ads.

PHP (FSock)

<?php

// Change to "test" to enable test mode
define("TAPIT_MODE", "live");

// Change to your Zone ID
define("TAPIT_ZONE", "PLACE YOUR ZONE ID HERE");

function tapit_ad()
{
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $ip = (stristr($ua,"opera mini") && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER))
        ? trim(end(split(",", $_SERVER['HTTP_X_FORWARDED_FOR'])))
        : $_SERVER['REMOTE_ADDR'];

    // Assemble Ad Request URL
    $tapit_get  = 'zone='.urlencode(TAPIT_ZONEID);
    $tapit_get .= '&ip='.urlencode($ip);
    $tapit_get .= '&ua='.urlencode($ua);


    if( TAPIT_MODE == "test" ) {
      $tapit_get .= '&mode=test';
    }

    // Send the Request
    $tapit_request = @fsockopen('r.tapit.com', 80, $errno, $errstr, 1);
    if ($tapit_request) {
        stream_set_timeout($tapit_request, 3000);
        $query = "GET /adrequest.php?".$tapit_get." HTTP/1.0\r\n";
        $query .= "Host: r.tapit.com\r\n";
        $query .= "Connection: close\r\n";

        $query .= "\r\n";
        fwrite($tapit_request, $query);
        $tapit_info = stream_get_meta_data($tapit_request);
        $tapit_timeout = $tapit_info['timed_out'];
        $tapit_contents = "";
        $tapit_body = false;
        $tapit_head = "";
        while (!feof($tapit_request) && !$tapit_timeout) {
            $tapit_line = fgets($tapit_request);
            if(!$tapit_body && $tapit_line == "\r\n") $tapit_body = true;
            if(!$tapit_body) $tapit_head .= $tapit_line;
            if($tapit_body && !empty($tapit_line)) $tapit_contents .= $tapit_line;
            $tapit_info = stream_get_meta_data($tapit_request);
            $tapit_timeout = $tapit_info['timed_out'];
        }
        fclose($tapit_request);
        if (!preg_match('/^HTTP\/1\.\d 200 OK/', $tapit_head)) $tapit_timeout = true;
        if($tapit_timeout) return "";
        return $tapit_contents;
    }
}

?>
<?php

// Place this into your page where you want to display ads
echo tapit_ad();

?>

PHP (CURL)

<?php

// Change to "test" to enable test mode
define( "TAPIT_MODE", "live");

// Change to your Zone ID
define( "TAPIT_ZONE", "PLACE YOUR ZONE ID HERE" );

function tapit_ad()
{
    $ua = $_SERVER['HTTP_USER_AGENT'];
    $ip = (stristr($ua,"opera mini") && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER))
        ? trim(end(split(",", $_SERVER['HTTP_X_FORWARDED_FOR'])))
        : $_SERVER['REMOTE_ADDR'];

    // Assemble Ad Request URL
    $tapit_get  = 'zone='.urlencode(TAPIT_ZONE);
    $tapit_get .= '&ip='.urlencode($ip);
    $tapit_get .= '&ua='.urlencode($ua);

    if( TAPIT_MODE == "test") {
      $tapit_get .= '&mode=test';
    }

    // Send the Request
    $url = "http://r.tapit.com/adrequest.php?" . $tapit_get;
    $timeout = 3; //timeout in seconds
    $ch  = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $html=@curl_exec($ch);
    @curl_close($ch);
    if( $html!== false ) {
      return $html;
    }
    else {
      return "";
    }
}

?>
<?php

// Place this into your page where you want to display ads
echo tapit_ad();

?>

JSP

<%@ page import="java.util.*,java.io.*,java.net.*, java.security.*, java.math.*" %>
<%
// Place this into your page where you want to display ads
out.print(tapit_ad(request));

%>
<%!

// Change to "test" to enable test mode
private static final String TAPIT_MODE = "live";

// Change to your Zone ID
private static final String TAPIT_ZONE = "PLACE YOUR ZONE ID HERE";

private String tapit_ad(HttpServletRequest request)
{
    StringBuilder tapit_contents = new StringBuilder();
    try {

        // Assemble Ad Request URL
        StringBuilder tapit_get = new StringBuilder();
        tapit_get.append("zone=").append(TAPIT_ZONE);
        tapit_get.append("&ua=").append(URLEncoder.encode(request.getHeader("User-Agent"), "UTF-8"));
        tapit_get.append("&ip=").append(URLEncoder.encode(request.getRemoteAddr(),"UTF-8"));

        if(TAPIT_MODE.equals("test")) tapit_get.append("&test=1");

        // Send the Request
        BufferedReader tapit_reader = null;
        try {
            URL tapit_url = new URL("http://r.tapit.com/adrequest.php?");
            HttpURLConnection tapit_request = (HttpURLConnection)tapit_url.openConnection();
            tapit_request.setRequestMethod("POST");
            tapit_request.setDoOutput(true);
            tapit_request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            tapit_request.setRequestProperty("Content-Length", Integer.toString(tapit_get.length()));
            tapit_request.setConnectTimeout(3000);
            tapit_request.setReadTimeout(3000);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(tapit_request.getOutputStream()));
            writer.write(tapit_get.toString());
            writer.close();
            tapit_reader = new BufferedReader(new InputStreamReader(tapit_request.getInputStream()));
            for (String line = null; (line = tapit_reader.readLine()) != null;)
                tapit_contents.append(line);
        } catch(java.net.SocketTimeoutException ste) {
              tapit_contents.append("");
        } catch (Exception e) {

        } finally {
            try {
                if(tapit_reader != null) {
                    tapit_reader.close();
                }
            } catch(Exception e) {

            }
        }
    } catch(Exception ex) {

    }
    return tapit_contents.toString();
}
%>

ASP

<%
'Place this into your page where you want to display ads
Response.Write(tapit_ad())

%>

<script runat="server" language="vbs">
    Function tapit_ad()

        'Change to "test" to enable test mode
        Dim TAPIT_MODE
        TAPIT_MODE = "live"

        'Change to your Zone ID
        Dim TAPIT_ZONE
        TAPIT_ZONE = "PLACE YOUR ZONE ID HERE"

        'Assemble Ad Request URL
        On Error Resume Next
        Dim tapit_get
        tapit_get = "zone=" & TAPIT_ZONE
        tapit_get = tapit_get & "&ua=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT"))
        tapit_get = tapit_get & "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR"))
        
        If TAPIT_MODE = "test" Then tapit_get = tapit_get & "&mode=test" End If

        'Send the Request
        Response.Buffer = True
        Dim tapit_request
        Set tapit_request = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
        tapit_request.setTimeouts 3000, 3000, 3000, 3000
        tapit_request.Open "GET", "http://r.tapit.com/adrequest.php?" & tapit_get, False
        tapit_request.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
        tapit_request.Send
        tapit_ad = tapit_request.responseText
        Set tapit_request = Nothing
        Err.clear
        If Err.number <> 0 Then
          tapit_ad = ""
        End If
    End Function
</script>

CGI/PERL

use LWP::UserAgent;
use URI::URL;

# Place this into your page where you want to display ads
print tapit_ad();

sub tapit_ad
{
    # Change to "test" to enable test mode
    my $tapit_mode = 'live';

    # Change to your Zone ID
    my $tapit_zone = 'PLACE YOUR ZONE ID HERE';

    # Assemble Ad Request URL
    my %tapit_get = (
        'zone'        => $tapit_zone,
        'ua'          => $ENVHTTP_USER_AGENT,
        'ip'          => $ENVREMOTE_ADDR
    );

    $tapit_getmode = 'test' if($tapit_mode eq 'test');

    # Send the Request
    my $tapit_uri = URI::URL->new("http://r.tapit.com/adrequest.php");
    my $tapit_request = LWP::UserAgent->new;
    $tapit_request->timeout(3);
    my $tapit_response = $tapit_request->post($tapit_uri, \%tapit_get);
    if ($tapit_response->is_error()) {
        return "";
    } else {
        return $tapit_response->content();
    }
}

VB.NET

<%
    'Place this into your page where you want to display ads
    Response.Write(tapit_ad())

%>

<script runat="server">
    Function tapit_ad() As String

        'Change to "test" to enable test mode
        Dim TAPIT_MODE As String = "live"

        'Change to your Zone ID
        Dim TAPIT_MODE As String = "PLACE YOUR ZONE ID HERE"

        'Assemble Ad Request URL
        Dim tapit_get As StringBuilder = New StringBuilder()
        tapit_get.Append("zone=").Append(TAPIT_ZONE)
        tapit_get.Append("&ua=").Append(HttpUtility.UrlEncode(Request.UserAgent, Encoding.UTF8))
        tapit_get.Append("&ip=").Append(HttpUtility.UrlEncode(Request.UserHostAddress, Encoding.UTF8))

        If TAPIT_MODE = "test" Then tapit_get.Append("&test=1")

        'Send the Request
        Dim tapit_contents As StringBuilder = New StringBuilder()
        Try
            Dim tapit_get_bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(tapit_get.ToString)
            Dim tapit_request As System.Net.WebRequest = System.Net.WebRequest.Create("http://r.tapit.com/adrequest.php")
            tapit_request.Method = "POST"
            tapit_request.ContentType = "application/x-www-form-urlencoded"
            tapit_request.ContentLength = tapit_get_bytes.Length()
            tapit_request.Timeout = 3000
            Dim tapit_os As System.IO.Stream = tapit_request.GetRequestStream()
            tapit_os.Write(tapit_get_bytes, 0, tapit_get_bytes.Length())
            tapit_os.Close()
            Dim tapit_response As System.Net.HttpWebResponse = CType(tapit_request.GetResponse, System.Net.HttpWebResponse)
            Dim tapit_ios As System.IO.Stream = tapit_response.GetResponseStream()
            Dim tapit_reader As System.IO.StreamReader = New System.IO.StreamReader(tapit_ios)
            tapit_contents.Append(tapit_reader.ReadToEnd())
        Catch tapit_e As Exception
            tapit_contents.Append("")
        End Try
        Return tapit_contents.ToString()
    End Function
</script>

C#

<%
// Place this into your page where you want to display ads
Response.Write(tapit_ad());

%>
<script runat="server">

// Change to "test" to enable test mode
const string TAPIT_MODE = "live";

// Change to your Zone ID
const string TAPIT_ZONE = "PLACE YOUR ZONE ID HERE";

string tapit_ad()
{
    // Assemble Ad Request URL
    StringBuilder tapit_get = new StringBuilder();
    tapit_get.Append("zone=").Append(TAPIT_ZONE);
    tapit_get.Append("&ua=").Append(HttpUtility.UrlEncode(Request.UserAgent, Encoding.UTF8));
    tapit_get.Append("&ip=").Append(HttpUtility.UrlEncode(Request.UserHostAddress, Encoding.UTF8));

    if (TAPIT_MODE == "test") tapit_get.Append("&mode=test");

    // Send the Request
    StringBuilder tapit_contents = new StringBuilder();
    try {
        byte[] tapit_get_bytes = System.Text.Encoding.UTF8.GetBytes(tapit_get.ToString());
        System.Net.WebRequest tapit_request  = System.Net.WebRequest.Create("http://r.tapit.com/adrequest.php?");
        tapit_request.Method = "POST";
        tapit_request.ContentType = "application/x-www-form-urlencoded";
        tapit_request.ContentLength = tapit_get_bytes.Length;
        tapit_request.Timeout = 3000;
        System.IO.Stream tapit_os = tapit_request.GetRequestStream();
        tapit_os.Write(tapit_get_bytes, 0, tapit_get_bytes.Length);
        tapit_os.Close();
        System.Net.HttpWebResponse tapit_response = (System.Net.HttpWebResponse)tapit_request.GetResponse();
        System.IO.StreamReader tapit_reader = new System.IO.StreamReader(tapit_response.GetResponseStream());
        tapit_contents.Append(tapit_reader.ReadToEnd().Trim());
    } catch (Exception) {
        tapit_contents.Append("");
    }
    return tapit_contents.ToString();
}
</script>

Ruby on Rails

<%
require 'net/http'
require 'uri'
require 'md5'

def tapit_ad(request)

    # Change to "test" to enable test mode
    tapit_mode   = "live"

    # Change to your Zone ID
    tapit_zone = "PLACE YOUR ZONE ID HERE"

    # Assemble Ad Request URL
    tapit_get = {}
    tapit_get["zone"]        = tapit_zone
    tapit_get["ua"]          = request.user_agent
    tapit_get["ip"]          = request.remote_ip
  
    tapit_get["mode"]        = "test" if tapit_get == "test"

    # Send the Request
    begin
        tapit_uri = URI.parse("http://r.tapit.com/adrequest.php")
        tapit_request = Net::HTTP::Post.new(tapit_uri.path)
        tapit_request.set_form_data(tapit_get)
        tapit_conn = Net::HTTP.new(tapit_uri.host, tapit_uri.port)
        tapit_conn.read_timeout = 3.0
        tapit_conn.open_timeout = 3.0
        tapit_response = tapit_conn.start {|tapit_http| tapit_http.request(tapit_request) }
        tapit_contents = tapit_response.body
    rescue Timeout::Error => te
        tapit_contents = ""
    rescue
        end
    tapit_contents
end
%>
<%=
    # Place this into your page where you want to display ads
    tapit_ad(request)
%>

JavaScript (Client Side)

Place this into your page where you want to display ads:
Note: Change "mode" to "test" to enable test mode

<script type="text/javascript" src="http://d2bgg7rjywcwsy.cloudfront.net/js/tapit.js"></script>
<script type="text/javascript">
  adrequest( {"zone": "PLACE YOUR ZONE ID HERE", "mode": "live", "location": false, "source": "device"} );
</script>

JavaScript for Non-Mobile Sites (Client Side)

Place this into your page where you want to display ads:
Note: Change "mode" to "test" to enable test mode

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script src="http://d2bgg7rjywcwsy.cloudfront.net/js/jquery.transform.js"></script>
    <script src="http://d2bgg7rjywcwsy.cloudfront.net/js/mdetect.js"></script>
    <script src="http://d2bgg7rjywcwsy.cloudfront.net/js/nonmobile.js"></script>
    <div id="tapitBanner"  style="width:320px; height:50px; left:0; position:absolute; display:none;">
      <style>
      #tapitBanner img {
        position: absolute;
        top: 0; left: 0;
        width: 100%;
        height: 100%;
      }
      </style>
      <script type="text/javascript" src="http://d2bgg7rjywcwsy.cloudfront.net/js/tapit.js"></script>
      <script type="text/javascript">
        if( DetectSmartphone() || DetectTierTablet() ) { adrequest( {"zone": "PLACE YOUR ZONE ID HERE", "location": false, "mode": "live" } ); }
      </script>
    </div>
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk