before running this example.'); } function public_api_get(string $baseUrl, string $path, string $apiKey, string $requestId): array { $curl = curl_init($baseUrl . $path); curl_setopt_array($curl, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $apiKey, 'X-Request-Id: ' . $requestId, ], ]); $body = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($body === false) { throw new RuntimeException(curl_error($curl)); } curl_close($curl); $decoded = json_decode($body, true, flags: JSON_THROW_ON_ERROR); if ($status < 200 || $status >= 300) { throw new RuntimeException('Public API problem JSON ' . $decoded['code'] . ' (' . $decoded['requestId'] . '): ' . $decoded['detail']); } return $decoded; } $fields = rawurlencode('productCode,description,price'); $page = public_api_get($baseUrl, "/v1/products?fields={$fields}&limit=50", $apiKey, 'php-products-page-1'); foreach ($page['products'] as $product) { echo $product['productCode'] . ' ' . $product['description'] . ' ' . $product['price'] . PHP_EOL; } if (!empty($page['nextCursor'])) { $cursor = rawurlencode($page['nextCursor']); $next = public_api_get($baseUrl, "/v1/products?fields={$fields}&limit=50&cursor={$cursor}", $apiKey, 'php-products-page-2'); echo 'Fetched ' . count($next['products']) . ' products from the nextCursor page.' . PHP_EOL; }