dev3 ·
I need to call a REST API from PHP with a JSON body and a Bearer token, read the response, and handle timeouts or non-200 responses. My attempts return an empty string or false.
0
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
I need to call a REST API from PHP with a JSON body and a Bearer token, read the response, and handle timeouts or non-200 responses. My attempts return an empty string or false.
Use curl_init with the right options, check both transport errors and HTTP status.
function apiPost(string $url, array $payload, string $token): array
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
CURLOPT_RETURNTRANSFER => true, // return body instead of printing it
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'Authorization: Bearer ' . $token,
],
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 20,
CURLOPT_FOLLOWLOCATION => false,
]);
$body = curl_exec($ch);
if ($body === false) {
$err = curl_error($ch);
curl_close($ch);
throw new RuntimeException("cURL error: $err");
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
if ($status >= 400) {
throw new RuntimeException("API returned HTTP $status: " . ($data['message'] ?? $body));
}
return $data;
}
Why you got false or empty output
CURLOPT_RETURNTRANSFER the response is printed and curl_exec returns true.curl.cainfo in php.ini to a current cacert.pem. Do not disable verification (CURLOPT_SSL_VERIFYPEER => false) in production, it removes the protection of HTTPS.CURLOPT_POSTFIELDS posts multipart/form-data, not JSON. Encode it yourself.Content-Type: application/json makes many APIs ignore the body.Pitfalls
composer require guzzlehttp/guzzle), which wrap all of this.Convert a command line request to PHP with the curl to code tool.