[["sku" => $sku, "quantity" => 1]]], JSON_THROW_ON_ERROR); $headers[] = "Idempotency-Key: " . $checkout; $headers[] = "Content-Type: application/json"; $path = "/v1/orders"; } elseif ($mode === "status") { $id = getenv("SPXRK_ORDER_ID") ?: ""; if (!preg_match('/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/iD', $id)) { throw new InvalidArgumentException("Set SPXRK_ORDER_ID to an order UUID"); } $path = "/v1/orders/" . $id; } elseif ($mode !== "catalog") { throw new InvalidArgumentException("Choose catalog, create or status"); } $responseHeaders = []; $ch = curl_init("https://spxrk.org" . $path); curl_setopt_array($ch, [ CURLOPT_HTTPHEADER => $headers, CURLOPT_USERAGENT => "SPXRK-PHP-Example/0.1.0", CURLOPT_FOLLOWLOCATION => false, CURLOPT_TIMEOUT => 15, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADERFUNCTION => static function ($ch, $line) use (&$responseHeaders): int { $header = explode(":", $line, 2); if (count($header) === 2) $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]); return strlen($line); } ]); if ($body !== null) curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $response = curl_exec($ch); if ($response === false) throw new RuntimeException("SPXRK transport error " . curl_errno($ch)); $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); if ($status < 200 || $status >= 300) { $error = json_decode($response, true)["error"]["code"] ?? "unclassified"; throw new RuntimeException(sprintf("HTTP %d %s; request=%s retry-after=%s", $status, $error, $responseHeaders["x-request-id"] ?? "-", $responseHeaders["retry-after"] ?? "-")); } $data = json_decode($response, true, flags: JSON_THROW_ON_ERROR); if ($mode === "catalog") { foreach ($data["data"] as $product) echo $product["sku"], " ", $product["name"], PHP_EOL; } else { echo $data["id"], " ", $data["state"], PHP_EOL; }