Target.php 6.58 KB
Newer Older
Pedro Monjo's avatar
Pedro Monjo committed
1
2
3
4
5
<?php

namespace Adobe;

/**
6
7
 * Target class.
 * Using delivery API 1.0
Pedro Monjo's avatar
Pedro Monjo committed
8
9
 */
class Target {
10
11
12
13

	const CHANNEL_WEB = 'web';
	const CHANNEL_MOBILE = 'mobile';

14
15
16
17
18
	/**
	 * VERSION
	 */
	const VERSION = "2.6.0";

Pedro Monjo's avatar
Pedro Monjo committed
19
20
21
22
23
24
25
26
27
28
29
	/**
	 * ECID object
	 */
	private $ecid;
	
	/**
	 * Link to configuration
	 */
	private $config;
	
	/**
30
	 * Last request to Target
Pedro Monjo's avatar
Pedro Monjo committed
31
	 */
32
	private $request;
Pedro Monjo's avatar
Pedro Monjo committed
33
34
	
	/**
35
	 * Last response from Target
Pedro Monjo's avatar
Pedro Monjo committed
36
	 */
37
	private $response;
Pedro Monjo's avatar
Pedro Monjo committed
38

Pedro Monjo's avatar
Pedro Monjo committed
39
40
41
42
	/**
	 * Request ID
	 */
	private $request_id;
43
44
45
46
47

	/**
	 * Session ID
	 */
	private $session_id;
Pedro Monjo's avatar
Pedro Monjo committed
48
	
49
50
51
52
53
54
55
56
57
58
	/**
	 * User agent of the browser
	 */
	private $user_agent;
	
	/**
	 * IP address of the client
	 */
	private $ip;
	
Pedro Monjo's avatar
Pedro Monjo committed
59
60
61
62
	/**
	 * Host
	 */
	private $host;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

	/**
	 * Channel
	 */
	private $channel;

	/**
	 * URL
	 */
	private $url;

	/**
	 * Referrer
	 */
	private $referrer;

	/**
	 * at_property
	 */
	private $at_property;
Pedro Monjo's avatar
Pedro Monjo committed
83
	
Pedro Monjo's avatar
Pedro Monjo committed
84
85
86
	/**
	 * Constructor
	 */
87
	public function __construct($config,$ecid,$session_id,$ip_client,$channel) {
Pedro Monjo's avatar
Pedro Monjo committed
88
89
		$this->config = $config;
		$this->ecid = $ecid;
90
		$this->session_id = $session_id;
91
		$this->ip = $ip_client;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		$this->channel = $channel;
	}

	/**
	 * Set User-Agent
	 */
	public function setUserAgent($user_agent) {
		$this->user_agent = $user_agent;
	}

	/**
	 * Set Host
	 */
	public function setHost($host) {
Pedro Monjo's avatar
Pedro Monjo committed
106
		$this->host = $host;
Pedro Monjo's avatar
Pedro Monjo committed
107
108
	}

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
	/**
	 * Set URL
	 */
	public function setURL($url) {
		$this->url = $url;
	}

	/**
	 * Set Referrer
	 */
	public function setReferrer($referrer) {
		$this->referrer = $referrer;
	}

	/**
	 * Set at_property
	 */
	public function setATProperty($at_property) {
		$this->at_property = $at_property;
	}

Pedro Monjo's avatar
Pedro Monjo committed
130
131
132
	/**
	 * Configure curl for the call to the ECID service
	 */
133
	private function curlConfigure($curl) {
Pedro Monjo's avatar
Pedro Monjo committed
134
		$curlopts = array(
135
			CURLOPT_URL 			=> $this->getRequestURL($curl),
Pedro Monjo's avatar
Pedro Monjo committed
136
			CURLOPT_RETURNTRANSFER 	=> 1,
Pedro Monjo's avatar
Pedro Monjo committed
137
			CURLOPT_COOKIEFILE 		=> '',
138
			CURLOPT_USERAGENT 		=> $this->user_agent,
139
			CURLOPT_POSTFIELDS		=> $this->getRequestJSON(),
140
			CURLOPT_PROTOCOLS		=> CURLPROTO_HTTP|CURLPROTO_HTTPS,
Pedro Monjo's avatar
Pedro Monjo committed
141
142
143
144
145
			CURLOPT_HTTPHEADER		=> array(
				'Cache-Control: no-cache',
				'Content-Type: application/json'
			)
		);
146
147
148
		if (isset($this->ip) && $this->ip != "127.0.0.1") {
			$curlopts[CURLOPT_HTTPHEADER][] = 'X-Forwarded-For: ' . $this->ip;
		}
Pedro Monjo's avatar
Pedro Monjo committed
149
150
151
152
153
154
		curl_setopt_array($curl, $curlopts);
	}
	
	/**
	 * Generate the URL to call Target
	 */
155
156
	private function getRequestURL($curl) {
		$client_code = $this->config->getTargetConfig(Config::TARGET_CLIENTCODE);
Pedro Monjo's avatar
Pedro Monjo committed
157
		$url = 'http';
Pedro Monjo's avatar
Pedro Monjo committed
158
		if ($this->config->getMarketingCloudConfig(Config::MARKETINGCLOUD_SSL) === true) {
Pedro Monjo's avatar
Pedro Monjo committed
159
			$url .= 's';
Pedro Monjo's avatar
Pedro Monjo committed
160
		}
Pedro Monjo's avatar
Pedro Monjo committed
161
		$url .= '://';
162
		$url .= $client_code;
163
		$url .= '.tt.omtrdc.net/rest/v1/delivery';
164
165
166
		$url .= '?client=' . curl_escape($curl,$client_code);
		$url .= '&sessionId=' . curl_escape($curl,$this->session_id);
		$url .= '&version=' . curl_escape($curl,Target::VERSION);
Pedro Monjo's avatar
Pedro Monjo committed
167
168
169
170
		return $url;
	}
	
	/**
171
	 * Generate the request for the Adobe Target Deliver API v1.0, in associative array format
172
	 * @ref https://developers.adobetarget.com/api/delivery-api/#section/Single-or-Batch-Delivery
Pedro Monjo's avatar
Pedro Monjo committed
173
	 */
174
175
176
	private function buildExecuteRequest($mboxes) {
		$this->request = array(
			'requestId' => bin2hex(random_bytes(20)),
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
			'id' => array(
				'marketingCloudVisitorId' => $this->ecid->getECID()
			),
			'context' => array(
				'channel' => $this->channel
			),
			'experienceCloud' => array(
				'audienceManager' => array(
					'locationHint' => $this->ecid->getDCSRegion(),
					'blob' => $this->ecid->getBlob()
				),
				'analytics' => array(
					'supplementalDataId' => $this->ecid->getSDID(),
					'logging' => "server_side" 
				)
			),
193
			'execute' => new \ArrayObject()
Pedro Monjo's avatar
Pedro Monjo committed
194
		);
195
		if ($this->user_agent) {
196
			$this->request['context']['userAgent'] = $this->user_agent;
197
		}
198
		if ($this->host) {
199
			$this->request['context']['browser'] = array(
200
201
202
203
				'host' => $this->host
			);
		}
		if ($this->url) {
204
			$this->request['context']['address'] = array(
205
206
207
				'url' => $this->url
			);
			if ($this->referrer) {
208
				$this->request['context']['address']['referringUrl'] = $this->referrer;
209
210
211
			}
		}
		if ($this->at_property) {
212
			$this->request['property'] = array(
213
214
215
				'token' => $this->at_property
			);
		}
216
217
218
219
220
221
222
223
224
		if ($mboxes && count($mboxes) > 0) {
			$this->request['execute']['mboxes'] = array();
			for ($i = 0; $i < count($mboxes) ; $i++) {
				$m = array(
					'index' => $i,
					'name' => $mboxes[$i]->getName()
				);
				array_push($this->request['execute']['mboxes'],$m);
			}
Pedro Monjo's avatar
Pedro Monjo committed
225
		}
226
227
228
		else {
			$this->request['execute']['pageLoad'] = new \ArrayObject();
		}
Pedro Monjo's avatar
Pedro Monjo committed
229
	}
Pedro Monjo's avatar
Pedro Monjo committed
230
231
232
233

	/**
	 * Parse the HTTP response.
	 */
234
235
236
	private function parseResponse($curl,$mboxes,$json_response) {
		$this->response = json_decode($json_response,TRUE);
		if ($this->response === NULL) {
237
			throw new \Exception("Adobe\\Target\\parseResponse: Could not decode JSON");
Pedro Monjo's avatar
Pedro Monjo committed
238
		}
239
		$this->request_id = $this->response['requestId'];
240
		// For each mbox in the response, find the corresponding mbox in the request and set the content
241
242
243
244
245
246
		if ($mboxes && count($mboxes) > 0) {
			foreach ($this->response['execute']['mboxes'] as $mbox_response) {
				$index = $mbox_response['index'];
				if (isset($mbox_response['options'])) {
					$mboxes[$index]->setOptions($mbox_response['options']);
				}
247
			}
Pedro Monjo's avatar
Pedro Monjo committed
248
249
250
		}
	}

Pedro Monjo's avatar
Pedro Monjo committed
251
	/**
252
253
	 * Executes request contents for the given mboxes. Prefetch is currently not supported.
	 * @param $mboxes An array of mboxes or null for a page load request (i.e. global mbox)
Pedro Monjo's avatar
Pedro Monjo committed
254
	 */
255
	public function execute($mboxes) {
Pedro Monjo's avatar
Pedro Monjo committed
256
		$curl = null;
257
258
259
260
261
262
263
264
265
266
267
		try {
			// Initialise curl
			$curl = curl_init();
			// Generate the body
			$this->buildExecuteRequest($mboxes);
			// Prepare HTTP request
			$this->curlConfigure($curl);
			// Make the HTTP request
			$json_response = curl_exec($curl);
			// Check for any errors
			if (curl_errno($curl)) {
Pedro Monjo's avatar
Pedro Monjo committed
268
				throw new \Exception("Adobe\\Target\\execute: " . curl_error($curl) . " (" . curl_errno($curl) . ")");
269
270
271
272
273
274
275
276
277
			}
			// Parse the result
			$this->parseResponse($curl,$mboxes,$json_response);
		}
		finally {
			// Free resources
			if ($curl) {
				curl_close($curl);
			}
278
		}
Pedro Monjo's avatar
Pedro Monjo committed
279
280
281
	}
	
	/**
282
	 * Get the last request to Target in associative array format
Pedro Monjo's avatar
Pedro Monjo committed
283
	 */
284
285
	public function getRequest() {
		return $this->request;
Pedro Monjo's avatar
Pedro Monjo committed
286
287
288
	}
	
	/**
289
	 * Get the last response to the Target API invokation, in associative array format
Pedro Monjo's avatar
Pedro Monjo committed
290
	 */
291
292
	public function getResponse() {
		return $this->response;
Pedro Monjo's avatar
Pedro Monjo committed
293
	}
294

295
	/**
296
297
	 * Get the last request to Target in JSON format
	 */
298
	public function getRequestJSON($pretty = false) {
299
300
301
		$flags = $pretty ? JSON_PRETTY_PRINT : 0;
		return json_encode($this->request,$flags);
	}
Pedro Monjo's avatar
Pedro Monjo committed
302
	
303
304
305
	/**
	 * Get the last response to the Target API invokation, in JSON format
	 */
306
	public function getResponseJSON($pretty = false) {
307
308
309
310
		$flags = $pretty ? JSON_PRETTY_PRINT : 0;
		return json_encode($this->response,$flags);
	}

Pedro Monjo's avatar
Pedro Monjo committed
311
}