| 1 |
<?php |
| 2 |
|
| 3 |
/// Interface to the Zanox API. |
| 4 |
class Dfr_ZanoxAPIClient |
| 5 |
{ |
| 6 |
public $base_url = "http://api.zanox.com/xml/2011-03-01"; |
| 7 |
|
| 8 |
function __construct($connect_id, $secret_key) { |
| 9 |
$this->_connect_id = $connect_id; |
| 10 |
$this->_secret_key = $secret_key; |
| 11 |
$this->_error = null; |
| 12 |
} |
| 13 |
|
| 14 |
function _prepare($verb, $params, $signed) { |
| 15 |
$qry = array(); |
| 16 |
if(is_array($params)) |
| 17 |
foreach($params as $k => $v) |
| 18 |
$qry[$k] = $v; |
| 19 |
$headers = null; |
| 20 |
if($signed) { |
| 21 |
$nonce = md5(microtime()); |
| 22 |
$date = gmdate("D, d M Y H:i:s T"); |
| 23 |
$sign = "GET/$verb/$date$nonce"; |
| 24 |
$sign = hash_hmac("sha1", $sign, $this->_secret_key, true); |
| 25 |
$sign = base64_encode($sign); |
| 26 |
$headers = array( |
| 27 |
"Authorization: ZXWS {$this->_connect_id}:$sign", |
| 28 |
"Nonce: $nonce", |
| 29 |
"Date: $date" |
| 30 |
); |
| 31 |
} else { |
| 32 |
$qry["connectid"] = $this->_connect_id; |
| 33 |
} |
| 34 |
|
| 35 |
$url = "{$this->base_url}/$verb/"; |
| 36 |
if($qry) { |
| 37 |
foreach($qry as $k => $v) |
| 38 |
$qry[$k] = rawurldecode($k) . "=" . rawurldecode($v); |
| 39 |
$url .= "?" . implode("&", $qry); |
| 40 |
} |
| 41 |
return array($url, $headers); |
| 42 |
} |
| 43 |
|
| 44 |
function _request($verb, $params=null, $signed=false) { |
| 45 |
list($url, $headers) = $this->_prepare($verb, $params, $signed); |
| 46 |
$c = curl_init($url); |
| 47 |
if($headers) |
| 48 |
curl_setopt($c, CURLOPT_HTTPHEADER, $headers); |
| 49 |
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
| 50 |
$xml = curl_exec($c); |
| 51 |
$err = curl_error($c); |
| 52 |
$errno = curl_errno($c); |
| 53 |
curl_close($c); |
| 54 |
if($err) |
| 55 |
return $this->_seterr("connection error: $errno $err"); |
| 56 |
if(!strlen($xml)) |
| 57 |
return $this->_seterr("connection error: empty response"); |
| 58 |
$xml = simplexml_load_string($xml); |
| 59 |
if($xml->getName() == "error") |
| 60 |
return $this->_seterr("api error: " . strval($xml->message)); |
| 61 |
return $xml; |
| 62 |
} |
| 63 |
|
| 64 |
function _seterr($err) { |
| 65 |
$this->_error = $err; |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
/// Get the last error message. |
| 70 |
function error() { |
| 71 |
return $this->_error; |
| 72 |
} |
| 73 |
|
| 74 |
/// Return the list of adspaces for this account. |
| 75 |
function adspaces() { |
| 76 |
$xml = $this->_request("adspaces", array("items" => 50), true); |
| 77 |
if(!$xml) |
| 78 |
return array(); |
| 79 |
$ls = array(); |
| 80 |
foreach($xml->adspaceItems->adspaceItem as $e) { |
| 81 |
$p = json_decode(json_encode($e), true); |
| 82 |
$p["id"] = intval($p["@attributes"]["id"]); |
| 83 |
$p["regions"] = current($p["regions"]); |
| 84 |
$p["categories"] = current($p["categories"]); |
| 85 |
unset($p["@attributes"]); |
| 86 |
$ls[] = $p; |
| 87 |
} |
| 88 |
return $ls; |
| 89 |
} |
| 90 |
|
| 91 |
/// Return the list of programs for the given adspace id. |
| 92 |
/// If $adspace_id is 0, return all programs. |
| 93 |
function programs($adspace_id=0) { |
| 94 |
$ls = array(); |
| 95 |
$total = null; |
| 96 |
for($page = 0; $page < 9999; $page++) { |
| 97 |
$params = array("items" => 50, "page" => $page); |
| 98 |
if($adspace_id) |
| 99 |
$params["adspace"] = $adspace_id; |
| 100 |
$xml = $this->_request("programapplications", $params, true); |
| 101 |
if(!$xml) |
| 102 |
return array(); |
| 103 |
if(!isset($xml->programApplicationItems->programApplicationItem)) { |
| 104 |
$this->_seterr("api error: no program application items"); |
| 105 |
return array(); |
| 106 |
} |
| 107 |
foreach($xml->programApplicationItems->programApplicationItem as $e) { |
| 108 |
$pid = intval($e->program["id"]); |
| 109 |
$aid = intval($e->adspace["id"]); |
| 110 |
$p = json_decode(json_encode($e), true); |
| 111 |
$p["id"] = $pid; |
| 112 |
$p["adspace_id"] = $aid; |
| 113 |
$p["active"] = $e->program["active"] == "true"; |
| 114 |
unset($p["@attributes"]); |
| 115 |
$ls[] = $p; |
| 116 |
} |
| 117 |
if(is_null($total)) |
| 118 |
$total = intval($xml->total); |
| 119 |
$total -= intval($xml->items); |
| 120 |
if($total <= 0) |
| 121 |
break; |
| 122 |
} |
| 123 |
return $ls; |
| 124 |
} |
| 125 |
|
| 126 |
/// Return the list of zmids for the given adspace as an array of arrays |
| 127 |
/// (program_id, adspace_id, zmid) |
| 128 |
function zmids($adspace_id) { |
| 129 |
$ps = $this->programs($adspace_id); |
| 130 |
if(!$ps) |
| 131 |
return array(); |
| 132 |
$ls = array(); |
| 133 |
foreach($ps as $p) { |
| 134 |
if($p["status"] != "confirmed") |
| 135 |
continue; |
| 136 |
$xml = $this->_request("admedia", array("program" => $p["id"], "purpose" => "productdeeplink")); |
| 137 |
if(!$xml) { |
| 138 |
$this->_error = null; |
| 139 |
continue; |
| 140 |
} |
| 141 |
if(intval($xml->items)) { |
| 142 |
foreach($xml->admediumItems->admediumItem->trackingLinks->trackingLink as $tt) { |
| 143 |
$ppv = strval($tt->ppv); |
| 144 |
preg_match('~\?(\w+)~', $ppv, $m); |
| 145 |
$ls[] = array($p["id"], intval($tt["adspaceId"]), $m[1]); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
return $ls; |
| 150 |
} |
| 151 |
|
| 152 |
/// Return a single zmid for the specific (adspace, program) combination. |
| 153 |
function zmid($adspace_id, $program_id) { |
| 154 |
$xml = $this->_request("admedia", array("program" => $program_id, "purpose" => "productdeeplink")); |
| 155 |
if(!$xml) |
| 156 |
return null; |
| 157 |
if(!intval($xml->items)) |
| 158 |
return null; |
| 159 |
foreach($xml->admediumItems->admediumItem->trackingLinks->trackingLink as $tt) { |
| 160 |
$ppv = strval($tt->ppv); |
| 161 |
preg_match('~\?(\w+)~', $ppv, $m); |
| 162 |
$aid = intval($tt["adspaceId"]); |
| 163 |
if($aid == $adspace_id) |
| 164 |
return $m[1]; |
| 165 |
} |
| 166 |
return null; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
} |
| 171 |
?> |