$credentialId, 'X-Request-Timestamp' => $ts, 'X-Request-Nonce' => $nonce, 'X-Request-Signature' => 'v1=' . hash_hmac('sha256', sb_canonical($ts, $nonce, $method, $target, $body), $secret)]; } function sb_request(string $method, string $target, string $body = ''): array { $headers = sb_sign(sb_decode_secret((string) getenv('SB_SECRET')), (string) getenv('SB_CREDENTIAL_ID'), $method, $target, $body); $lines = ['Accept: application/json', 'Accept-Encoding: identity']; foreach ($headers as $k => $v) $lines[] = "$k: $v"; if ($body !== '') $lines[] = 'Content-Type: application/json'; $ch = curl_init(rtrim((string) getenv('SB_API_BASE'), '/') . $target); curl_setopt_array($ch, [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => $lines, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_PROXY => '', CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 20]); if ($body !== '') curl_setopt($ch, CURLOPT_POSTFIELDS, $body); $resp = curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); if ($resp === false) throw new RuntimeException('transport error: outcome unknown for a POST, look it up'); return ['status' => $status, 'body' => $resp]; } if (PHP_SAPI === 'cli' && realpath($argv[0]) === __FILE__) { $cmd = $argv[1] ?? ''; if ($cmd === 'vectors') { $n = 0; foreach (json_decode((string) file_get_contents($argv[2]), true, 8, JSON_THROW_ON_ERROR) as $v) { $h = sb_sign(sb_decode_secret($v['secret_wire']), $v['credential_id'], $v['method'], $v['target'], $v['body'], $v['timestamp'], $v['nonce']); if ($h['X-Request-Signature'] !== $v['signature'] || sb_canonical($v['timestamp'], $v['nonce'], $v['method'], $v['target'], $v['body']) !== $v['canonical_message']) { fwrite(STDERR, "MISMATCH {$v['method']} {$v['target']}\n"); exit(1); } $n++; } echo "php: $n vectors OK\n"; } elseif ($cmd === 'get' || $cmd === 'post') { print_r(sb_request(strtoupper($cmd), $argv[2], $argv[3] ?? '')); } else { fwrite(STDERR, "usage: vectors | get | post \n"); exit(2); } }