Created
June 10, 2016 14:53
-
-
Save bactisme/cc6be5c9ffd98c6aace4d3bb4210bad4 to your computer and use it in GitHub Desktop.
Commission Junction DeepLink Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This log on CJ platform, get some info (account number) and construct a link with it. | |
// Licence: Beerware | |
class CJDeepLink { | |
var $uname; | |
var $pw; | |
var $pub; | |
public function __construct($uname, $pw, $pub = ""){ // pub can be from the first request | |
$this->uname = $uname; | |
$this->pw = $pw; | |
$this->cookieFile = "/tmp/cookieCJDeeplink".rand(99, 999999999); | |
} | |
public function getDeepLink($url){ | |
$this->connection(); // cookie !! | |
if ($this->pub){ | |
// tracking request = ; | |
$domain = $this->getTrackingDomain(); | |
if ($domain){ | |
$webid = $this->getWebsiteId(); | |
return "http://".$domain."/links/".$webid."/type/dlg/".$this->cleanUrl($url); | |
} | |
} | |
return null; | |
} | |
private function cleanUrl($link){ | |
if (strpos($link, '?') !== false){ // clean | |
$link = substr($link, 0, strpos($link, '?')); | |
} | |
if (strpos($link, '#') !== false){ // clean | |
$link = substr($link, 0, strpos($link, '#')); | |
} | |
return $link; | |
} | |
private function connection(){ | |
$url = "https://members.cj.com/member/foundation/memberlogin.json"; | |
$data = "uname=".urlencode($this->uname)."&pw=".urlencode($this->pw); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$tmp = curl_exec($ch); // execute the curl command | |
curl_close($ch); | |
$decode = json_decode($tmp, true); | |
if (isset($decode['contact']['company']['id'])){ | |
$this->pub = $decode['contact']['company']['id']; | |
} | |
} | |
private function getTrackingDomain(){ | |
$url = "https://members.cj.com/member/api/publisher/3592881/trackingDomain?_=".time(); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile); | |
$tmp = curl_exec($ch); // execute the curl command | |
curl_close($ch); | |
$decode = json_decode($tmp, true); | |
if (isset($decode['trackingDomain'])){ | |
return $decode['trackingDomain']; | |
} | |
return false; | |
} | |
private function getWebsiteId(){ | |
$url = "https://members.cj.com/member/publisher/3592881/websites.json?limit=100"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile); | |
$tmp = curl_exec($ch); // execute the curl command | |
curl_close($ch); | |
$decode = json_decode($tmp, true); | |
if (isset($decode['list'][0]["website"]["@id"])){ | |
return $decode['list'][0]["website"]["@id"]; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great snippet!
Also, your publisher id appears to be hard-coded into serveral of the urls.