API - General information¶
Access credentials¶
Create a new client from "Other » API Clients module", use generated ID and Key.
Authentication¶
Every request must include the following headers- X-API-ID: Required. supplied by Client, see Access credentials
- X-API-Hash: Required. MD5 Hash with concatenated Api key, URLl and Post data
- X-User-Token: Optional, depending on the URL. Unique token for guests/customers, see Token generation
Code sample:¶
$api_id = 1;
$api_key = 'testpass';
$hostname = 'https://example.com'; // no trailing slash
# fetch data
$url = '/sitemap/';
$post_data = array();
$user_token = null; // token not required here. If needed, get it from Your DB after Token generation
$extra_headers = array(
'X-API-ID: ' . $api_id,
'X-API-Hash: ' . md5($api_key . $url. http_build_query($post_data)),
'X-User-Token: ' . $user_token
);
header('Content-Type: application/json; charset=utf-8');
echo sendRequest($hostname . $url, $post_data, $extra_headers);
sendRequest function (cURL)¶
/**
* @param string $url
* @param array $postdata
* @return bool|mixed
*/
function sendRequest($url, $post_data = array(), $extra_headers = array()) {
if (!function_exists('curl_init')) {
//error_log("PHP Warning: CURL is not installed. curlPost function will now exit");
return false;
}
// init CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if (!empty($post_data) && is_array($post_data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
}
if (!empty($extra_headers) && is_array($extra_headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $extra_headers);
}
// get response
$output = curl_exec($ch);
if ($output === false) {
$error_code = curl_error($ch);
}
else {
$error_code = 'N/A';
}
// get headers
$headers = curl_getinfo($ch);
// close connection
curl_close($ch);
// if request is OK, return contents
if ($headers['http_code'] == 200 || $headers['http_code'] == 226) {
return $output;
}
// return an error
else {
//error_log("PHP Warning: Bad CURL header ({$headers['http_code']}) on URL: {$url}, error code: {$error_code}");
return false;
}
}
Succes message :
{ has_error: false, messages: { section: "sitemap" }, results: { categories: {}, manufacturers: {} } }
Error message :
{ has_error: true, messages: [ "Invalid API password" ], results: [ ] }
Invalid URL message :
{ has_error: true, messages: { redirect_code: 301, redirect_reason: "trim_multiple_slashes", redirect_url: "/sitemap/" }, results: [] }
Available in other languages: RO
Go to top