API docs
Op deze pagina vind je informatie over onze REST API voor het ophalen van voorraadgegevens. Deze API stelt je in staat om realtime voorraadgegevens van onze producten te integreren met jouw systemen.
Voorraad Export API
De Voorraad Export API biedt toegang tot productvoorraadgegevens in zowel JSON- als CSV-formaat.
REST API Endpoints
Voorraadgegevens ophalen
GET /wp-json/tcp/v1/stock
Parameters:
format
(optioneel): Antwoordformaat –json
(standaard) ofcsv
page
(optioneel): Paginanummer voor gepagineerde resultaten (alleen JSON-formaat) – standaard:1
per_page
(optioneel): Aantal items per pagina (alleen JSON-formaat) – standaard:100
, max:500
Voorbeeld:
- JSON-formaat (paginated):
https://tomacarparts.nl/wp-json/tcp/v1/stock?page=1&per_page=100
- CSV-formaat (alle producten):
https://tomacarparts.nl/wp-json/tcp/v1/stock?format=csv
Response headers (JSON-formaat):
Het JSON-antwoord bevat standaard WordPress REST API paginatieheaders:
X-WP-Total
: Totaal aantal productenX-WP-TotalPages
: Totaal aantal pagina’s- Link-headers voor vorige/volgende pagina’s
Belangrijk: Wij raden je sterk aan om de JSON API te gebruiken in plaats van de CSV-export. De JSON API biedt:
- Betere betrouwbaarheid en stabiliteit
- Betere error responses
- Paginatie voor het verwerken van grote hoeveelheden gegevens
- Betere prestaties
Voorbeeld: Alle pagina’s ophalen met de JSON API
Om alle voorraadgegevens met de JSON API te ophalen, kun je de volgende code gebruiken om alle pagina’s te doorlopen:
// JavaScript example with async/await and fetch
async function get_all_stock() {
let page = 1;
const per_page = 500; // Maximum number of items per page
let more_pages = true;
let all_products = [];
while (more_pages) {
console.log(`Fetching page ${page}...`);
try {
const response = await fetch(`https://yourwebsite.com/wp-json/tcp/v1/stock?page=${page}&per_page=${per_page}`);
// Check if the request was successful
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
// Get pagination information from headers
const total_pages = parseInt(response.headers.get('X-WP-TotalPages')) || 0;
// Get the data
const products = await response.json();
// Add current page products to our result
all_products = all_products.concat(products);
// Check if there are more pages
if (page >= total_pages) {
more_pages = false;
} else {
page++;
}
} catch (error) {
console.error('Error fetching stock data:', error);
more_pages = false; // Stop on errors
}
}
console.log(`Total ${all_products.length} products retrieved.`);
return all_products;
}
// Use the function
get_all_stock().then(products => {
console.log('All stock data:', products);
});
In PHP kun je het volgende voorbeeld gebruiken:
// PHP example with cURL
function get_all_stock() {
$base_url = 'https://yourwebsite.com/wp-json/tcp/v1/stock';
$per_page = 500; // Maximum number of items per page
$page = 1;
$more_pages = true;
$all_products = [];
while ($more_pages) {
$url = $base_url . '?page=' . $page . '&per_page=' . $per_page;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status_code !== 200) {
error_log("HTTP error: " . $status_code);
break;
}
// Find the X-WP-TotalPages header
preg_match('/X-WP-TotalPages: (\d+)/i', $header, $matches);
$total_pages = isset($matches[1]) ? (int)$matches[1] : 0;
// Decode the JSON data
$products = json_decode($body, true);
if (is_array($products)) {
$all_products = array_merge($all_products, $products);
}
// Check if there are more pages
if ($page >= $total_pages) {
$more_pages = false;
} else {
$page++;
}
}
return $all_products;
}
// Use the function
$products = get_all_stock();
echo "Total " . count($products) . " products retrieved.";
Voor grote datasets raden wij je aan om de resultaten pagina per pagina te verwerken in plaats van alle producten tegelijk in het geheugen te laden.