<?php
//PHP cURL extension is needed
define('API_URL', 'https://api.insplay.eu/');
define('API_METHOD', 'products');

define('API_KEY', 'Your API key here');
define('API_LANGUAGE', 'et_EE');

function getCURL($url, $fields = array(), $headers = array()) {
	$ch = curl_init();

	$options[CURLOPT_URL] = $url;
	$options[CURLOPT_POST] = count($fields);
	$options[CURLOPT_POSTFIELDS] = json_encode($fields);
	$options[CURLOPT_RETURNTRANSFER] = true;

	if (is_array($headers) and count($headers)) {
		foreach ($headers as $k => $v) {
			$options[$k] = $v;
		}
	}

	curl_setopt_array($ch, $options);

	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

$url = API_URL.API_METHOD;
$fields = array(
	'params' => array(
		'api_key' => API_KEY,
		'language' => API_LANGUAGE
	)
);
$headers = array(
	CURLOPT_HTTPHEADER => array('Content-Type: application/json')
);
$APIResult = json_decode(getCURL($url, $fields, $headers));

print_r($APIResult);

if (isset($API['result']) and isset($API['result']['data'])) {
	foreach ($API['result']['data'] as $product_id=>$val) {
		foreach ($val['additional_images'] as $k=>$v) {
			$fn = DATA_DIR.'product_'.$product_id.'_'.$k.'.jpg';
			$fp = fopen($fn, 'wb');
			fwrite($fp, base64_decode($val['image_base64']));
			fclose($fp);
			echo '<a href="'.$fn.'" target="_blank">'.$fn.'</a><br />';
		}
		$fn = DATA_DIR.'product_'.$product_id.'.jpg';
		$fp = fopen($fn, 'wb');
		fwrite($fp, base64_decode($val['main_image_base64']));
		fclose($fp);
		echo '<a href="'.$fn.'" target="_blank">'.$fn.'</a><br />';
	}
}
exit;
?>