-
Notifications
You must be signed in to change notification settings - Fork 3
/
API.php
138 lines (116 loc) · 3.9 KB
/
API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace AffiliTest;
require_once __DIR__ . '/../vendor/autoload.php';
use CurlX;
class API {
const ENDPOINTS = [
'main' => 'https://affilitest.com/',
'login' => 'user/login',
'test' => 'api/v1/test',
'compareToPreview' => 'api/v1/compareToPreview',
'appInfo' => 'api/v1/appInfo',
'callsLeft' => 'api/v1/callsLeft'
];
const COOKIE_PATH = '/tmp/affilitestCookies';
function __construct($apiKey = null, $concurrency = 10, $cookiesFile = API::COOKIE_PATH) {
$this->apiKey = $apiKey;
$this->cookiesFile = $cookiesFile;
$this->setupCurl();
}
function __destruct() {
$this->closeCurl();
}
public function closeCurl() {
if($this->curl) {
curl_close($this->curl);
}
$this->curl = null;
}
private function setupCurl() {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
if ($this->apiKey) {
curl_setopt($this->curl, CURLOPT_HTTPHEADER, [
'Authorization: AT-API '. $this->apiKey
]);
} else {
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiesFile);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookiesFile);
}
}
function setCookieFile($path) {
$this->cookiesFile = $path;
curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiesFile);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, $this->cookiesFile);
}
function login($email, $password) {
return $this->post('login', [
'email' => $email,
'password' => $password
]);
}
function test($url, $country, $device) {
return $this->post('test', [
'url' => $url,
'country' => $country,
'device' => $device
]);
}
function compareToPreview($url, $country, $device, $previewURL) {
return $this->post('compareToPreview', [
'url' => $url,
'country' => $country,
'device' => $device,
'previewURL' => $previewURL
]);
}
function appInfo($url, $package = null, $country = '') {
if (empty($url) && empty($package)) {
throw new Exception('Missing URL/Package in appInfo call');
}
$data = [
'country' => $country
];
if(!empty($url)) {
$data['url'] = $url;
return $this->get('appInfo', $data);
}
$data['package'] = $package;
return $this->get('appInfo', $data);
}
function callsLeft() {
return $this->get('callsLeft');
}
private function post($endpoint, $data) {
curl_setopt($this->curl, CURLOPT_URL, API::ENDPOINTS['main'] . API::ENDPOINTS[$endpoint]);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
$exec = curl_exec($this->curl);
if($exec === false) {
throw new Exception(curl_error($this->curl));
}
return json_decode($exec);
}
private function get($endpoint, $data = null) {
curl_setopt($this->curl, CURLOPT_POST, 0);
$payload = '';
if(!empty($data)) {
$payload = '?' . http_build_query($data);
}
curl_setopt($this->curl, CURLOPT_URL, API::ENDPOINTS['main'] . API::ENDPOINTS[$endpoint] . $payload);
$exec = curl_exec($this->curl);
if($exec === false) {
throw new Exception(curl_error($this->curl));
}
return json_decode($exec);
}
}
class Devices {
const ANDROID = 'android',
IPHONE = 'iphone',
IPAD = 'ipad',
DESKTOP = 'desktop',
WINDOWS_PHONE = 'winPhone',
BLACKBERRY = 'blackberry';
}