#!/usr/bin/env php createElement('urlset'); $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); $doc->appendChild($urlset); foreach ($endpoints as $endpoint) { $url = $doc->createElement('url'); $loc = $url->appendChild($doc->createElement('loc')); $urlText = $doc->createTextNode($baseUrl . $endpoint); $loc->appendChild($urlText); $url->appendChild($doc->createElement('lastmod', $nowDate)); $url->appendChild($doc->createElement('changefreq', 'monthly')); $url->appendChild($doc->createElement('priority', '0.8')); $urlset->appendChild($url); } return $doc->saveXML(); } /** * Consume all items from the given API listing endpoint. */ function getAllOfAtListEndpoint(string $endpoint, array $params): array { $count = 100; $offset = 0; $all = []; do { $endpoint = $endpoint . '?' . http_build_query(array_merge($params, ['count' => $count, 'offset' => $offset])); $resp = apiGetJson($endpoint); $total = $resp['total'] ?? 0; $new = $resp['data'] ?? []; array_push($all, ...$new); $offset += $count; } while ($offset < $total); return $all; } /** * Make a simple GET HTTP request to the API. */ function apiGet(string $endpoint): string { global $baseUrl, $clientId, $clientSecret; $url = rtrim($baseUrl, '/') . '/' . ltrim($endpoint, '/'); $opts = ['http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"]]; $context = stream_context_create($opts); return @file_get_contents($url, false, $context); } /** * Make a simple GET HTTP request to the API & * decode the JSON response to an array. */ function apiGetJson(string $endpoint): array { $data = apiGet($endpoint); return json_decode($data, true); } /** * DEBUG: Dump out the given variables and exit. */ function dd(...$args) { foreach ($args as $arg) { var_dump($arg); } exit(1); } /** * Alert of an error then exit the script. */ function errorOut(string $text) { echo "ERROR: " . $text; exit(1); }