// Created on savesnippets.com · https://savesnippets.com/BxyvlYNRpuRneu true, CURLOPT_POSTFIELDS => json_encode($body, JSON_THROW_ON_ERROR), CURLOPT_HTTPHEADER => array_merge([ 'Content-Type: application/json', 'Accept: application/json', ], $extraHeaders), CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $timeout, ]); $resp = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); curl_close($ch); if ($code < 200 || $code >= 300) { throw new RuntimeException("POST failed: HTTP $code — $resp"); } return json_decode($resp, true, 512, JSON_THROW_ON_ERROR); } $user = postJson('https://api.example.com/users', ['name' => 'Alice', 'email' => 'a@x.com']);