<?php 
	$product       = $this->getCurrentProduct();

	$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
	$storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
	$base_url      = trim($storeManager->getStore()->getBaseUrl(),"/");
	$urlInterface  = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
	$current_url   = $urlInterface->getCurrentUrl();
	$ipaddress     = "";

    if (getenv("HTTP_CLIENT_IP")) $ipaddress = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR")) $ipaddress = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("HTTP_X_FORWARDED")) $ipaddress = getenv("HTTP_X_FORWARDED");
    else if(getenv("HTTP_FORWARDED_FOR")) $ipaddress = getenv("HTTP_FORWARDED_FOR");
    else if(getenv("HTTP_FORWARDED")) $ipaddress = getenv("HTTP_FORWARDED");
    else if(getenv("REMOTE_ADDR")) $ipaddress = getenv("REMOTE_ADDR");
    else $ipaddress = "UNKNOWN";

    $affliate_cookie = (isset($_GET['af_id']) ? $_GET['af_id'] : (isset($_COOKIE["af_id"]) ? $_COOKIE["af_id"] : '') ); 
	$affiliateData = array(
		"product_id"       => $product->getId(),
		"af_id"            => $affliate_cookie,
		"ip"               => $ipaddress,
		"base_url"         => base64_encode($base_url),
		"current_page_url" => base64_encode($current_url),
		"script_name"      => "magento",
	);
	
	$request_headers = array();
	$request_headers[] = 'User-Agent: '. (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
	$request_headers[] = 'Referer: '. $current_url;

	$request = http_build_query($affiliateData);
	$curl = curl_init('__baseurl__integration/addClick?'.$request);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	
	curl_exec($curl);