· 7 years ago · Apr 25, 2018, 09:22 AM
1<?php
2
3define('API_URL', 'http://api.kadam.net/%action%.%method%?%params%');
4class Api {
5 private $appID;
6 private $secret;
7 private $token;
8
9
10 public function __construct($appID, $secret) {
11 $this->appID = $appID;
12 $this->secret = $secret;
13 }
14
15 /**
16 * create campaign
17 * @param $type
18 * @param $cpType
19 * @param $learningPayMode
20 * @param $cpaMode
21 * @param $leadCost
22 * @param $name
23 * @param $geo
24 * @param $tags
25 * @param $categories
26 * @param $url
27 * @param $gender
28 * @param $age
29 * @param $platform
30 * @param $browser
31 * @param bool $learning
32 * @param array $folders
33 * @param array $black
34 * @param array $white
35 * @return array|mixed
36 * @throws Exception
37 */
38 public function createCampaign($type, $cpType, $learningPayMode, $cpaMode, $leadCost, $name, $geo, $tags, $categories, $url, $gender, $age, $platform, $browser, $learning = true, $folders = [], $black = [], $white = []) {
39 $campaign_data = [
40 'client_id' => $this->appID,
41 'ad_format' => $type,
42 'cost_type' => $cpType,
43 'cpa_mode' => $cpaMode,
44 'learning_pay_mode' => $learningPayMode,
45 'cost' => $leadCost,
46 'name' => $name,
47 'regions' => $geo,
48 'link_url' => $url,
49 'real_url' => $url,
50 'sex' => $gender,
51 'age' => $age,
52 'platforms' => $platform,
53 'browsers' => $browser,
54 'learning' => $learning
55 ];
56
57 if(!empty($black)) {
58 $campaign_data['black_list'] = $black;
59 }
60
61 if(!empty($white)) {
62 $campaign_data['white_list'] = $white;
63 }
64
65 if(!empty($categories))
66 $campaign_data['categories'] = $categories;
67
68 if(!empty($tags))
69 $campaign_data['tags'] = $tags;
70
71 if(!empty($folders))
72 $campaign_data['folders'] = $folders;
73
74 $url = $this->_prepare_url('ads.campaigns.put', [
75 'data' => json_encode($campaign_data)
76 ]);
77
78 $urlContents = explode("?", $url);
79
80 echo $urlContents[0]."\n\n\n";
81 echo $urlContents[1];
82
83 $result = $this->_execute_request($urlContents[0], $urlContents[1]);
84
85 if (empty($result[0]) || !intval($result[0])) {
86 throw new Exception("Error creating campaign: " . json_encode($result));
87 }
88
89 return $result[0];
90 }
91
92
93 /**
94 * get auth token
95 * @throws Exception
96 */
97 public function auth() {
98 $auth_url = $this->_prepare_url('auth.token', ['secret_key' => $this->secret], false);
99 $auth = $this->_execute_request($auth_url);
100 if (!isset($auth['access_token'])) {
101 throw new Exception('Access denied');
102 }
103 $this->token = $auth['access_token'];
104 }
105
106
107 /**
108 * process and prepare request url
109 * @param $action_and_method
110 * @param array $params
111 * @param bool $signature
112 * @return mixed
113 */
114 private function _prepare_url($action_and_method, array $params, $signature = true) {
115 $params_string = $this->_process_params($params, $signature);
116 $pattern = [
117 '/%action%\.%method%/',
118 '/%params%/'
119 ];
120 $replacement = [
121 $action_and_method,
122 $params_string
123 ];
124
125 return preg_replace($pattern, $replacement, API_URL);
126 }
127
128 /**
129 * sort params
130 * @param array $params
131 * @param bool $signature
132 * @return string
133 */
134 private function _process_params(array $params, $signature = true) {
135 $params = array_merge([
136 'app_id' => $this->appID,
137 'client_id' => $this->appID,
138 ], $params);
139
140 ksort($params);
141
142 $url_params = [];
143 foreach ($params as $param => $value) {
144 $url_params[] = $param . '=' . urlencode($value);
145 }
146
147 $params_string = implode('&', $url_params);
148 $params_string .= $this->_prepare_signature($params_string, $signature);
149
150 return $params_string;
151 }
152
153 /**
154 * add signature param
155 * @param string $params_string
156 * @param bool $signature
157 * @return string
158 */
159 private function _prepare_signature($params_string = '', $signature = true) {
160 return $signature ? '&signature=' . md5($params_string . $this->token) : '';
161 }
162
163
164 /**
165 * execute request
166 * @param $url
167 * @param bool $post
168 * @return array|mixed
169 * @throws Exception
170 */
171 private function _execute_request($url, $post = false) {
172 $curl = curl_init();
173 curl_setopt($curl, CURLOPT_URL, $url);
174 curl_setopt($curl, CURLOPT_HEADER, 0);
175 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
176 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
177 if ($post) {
178 curl_setopt($curl, CURLOPT_POST, 1);
179 curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
180 }
181
182 $response = curl_exec($curl);
183
184 $json_response = json_decode($response, true);
185
186 // invalid response
187 if (!is_array($json_response)) {
188 throw new Exception("Response error (not JSON):\n".print_r($response, true));
189 }
190
191 // error in method
192 if (isset($json_response[0]) AND is_array($json_response[0]) AND isset($json_response[0]['error'])) {
193 throw new Exception($json_response[0]['error']['msg'] . " (" . $url . ")");
194 }
195
196 return $json_response;
197 }
198}
199
200// create api instance
201$api = new Api(345367, "45yhrtyuh56jhe56uhe56ue56");
202
203try {
204 // get token
205 $api->auth();
206
207 // create campaign
208 $cid = $api->createCampaign(10, 0, 0, 0, 0, "test", [4], [], [127 => 3.1], "http://google.com", 2, 2, 2, 2,true,[],[565656,676767,787878]);
209}
210catch (Exception $ex) {
211 print_r($ex);
212}