'https://marketplace.posticy.com/api/v1/', 'headers' => ['X-API-Key' => $key, 'Accept' => 'application/json'], 'timeout' => 30, 'connect_timeout' => 10, 'allow_redirects' => false, 'http_errors' => false, ]); try { // One small read; no automatic retries or full-catalog crawl. $response = $client->get('marketplace/websites', [ 'query' => ['per_page' => 5, 'page' => 1, 'min_dr' => 30], ]); $status = $response->getStatusCode(); if ($status !== 200) { if ($status === 429) { $error = json_decode((string) $response->getBody(), true); $wait = is_array($error) ? ($error['retry_after'] ?? null) : null; if (is_numeric($wait) && is_finite((float) $wait) && $wait >= 0 && $wait <= 86400) { fwrite(STDERR, 'Rate limited. Wait ' . (int) ceil($wait) . " seconds before retrying.\n"); exit(1); } } throw new RuntimeException('Request failed'); } $body = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR); if (!is_array($body) || ($body['success'] ?? false) !== true || !is_array($body['data'] ?? null) || !array_is_list($body['data'])) { throw new RuntimeException('Unexpected response'); } // Validate the entire page before printing anything, avoiding partial results. $lines = []; foreach ($body['data'] as $website) { if (!is_array($website) || !is_string($website['domain'] ?? null) || $website['domain'] === '' || !is_array($website['services'] ?? null)) { throw new RuntimeException('Unexpected listing'); } $offers = []; foreach (['guest_post', 'link_insertion'] as $name) { $service = $website['services'][$name] ?? null; if (!is_array($service) || !is_bool($service['available'] ?? null) || !array_key_exists('price', $service) || !array_key_exists('currency', $service)) { throw new RuntimeException('Unexpected service'); } if ($service['available']) { if (!is_numeric($service['price']) || !is_finite((float) $service['price']) || $service['price'] <= 0 || !in_array($service['currency'], ['EUR', 'USD', 'GBP'], true)) { throw new RuntimeException('Unexpected service price'); } $offers[] = $name . ': ' . $service['price'] . ' ' . $service['currency']; } elseif ($service['price'] !== null || $service['currency'] !== null) { throw new RuntimeException('Unexpected unavailable service'); } } // CLI output only; escape returned text if you render it in HTML. $lines[] = $website['domain'] . ' | ' . ($website['link_type'] ?? 'Unknown link type') . ' | ' . implode(', ', $offers); } if ($lines) { echo implode(PHP_EOL, $lines) . PHP_EOL; } } catch (Throwable $error) { // Do not log request headers, keys or raw exception messages. fwrite(STDERR, "Catalog request failed. Check access, input and connectivity.\n"); exit(1); }