$_SERVER['HTTP_' . strtoupper(str_replace('-', '_', $n))] ?? null; $si = $h('signature-input'); $headers = []; if ($si && preg_match_all('/"([a-z0-9-]+)"/', $si, $m)) { foreach ($m[1] as $n) { if (strpos($n, 'signature') !== 0) { $headers[$n] = (string)$h($n); } } } $uri = $_SERVER['REQUEST_URI'] ?? '/'; $body = array_filter([ 'stage' => $stage, 'method' => $_SERVER['REQUEST_METHOD'] ?? 'GET', 'host' => $_SERVER['HTTP_HOST'] ?? '', 'path' => strtok($uri, '?'), 'query' => parse_url($uri, PHP_URL_QUERY), 'user_agent' => $h('user-agent'), 'ip' => $_SERVER['REMOTE_ADDR'] ?? null, 'signature' => $h('signature'), 'signature_input' => $si, 'signature_agent' => $h('signature-agent'), 'forwarded' => $h('forwarded'), 'headers' => $headers ?: null, 'browser_signals' => is_string($_POST['wilby_signals'] ?? null) ? $_POST['wilby_signals'] : null, // from agent-signals.js on your checkout page ], fn($v) => $v !== null && $v !== ''); $ch = curl_init('https://dev.usewilby.com/v1/agent-checks'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT_MS => $wait_ms, CURLOPT_CONNECTTIMEOUT_MS => $wait_ms, CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('WILBY_WEBSITE_KEY'), 'Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode($body), ]); $out = curl_exec($ch); $ok = curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200; curl_close($ch); return ($out !== false && $ok) ? json_decode($out, true) : null; // null: go ahead as usual } // 1. Pages: PHP can't easily send in the background, so a short wait (or use the Cloudflare Worker for pages). // wilby_agent_check('page', 150); // 2. At checkout: wait at most 300 ms. $check = wilby_agent_check('checkout', 300); if ($check && $check['allowed'] === false) { http_response_code(403); exit($check['message']); } // 3. Create the order with Wilby as you do today, adding "agent_check_id" => $check['id'] ?? null. // If the answer has customer_approval.url, send it to the customer: they approve the order before you charge.