Reputation: 20654
How can I get the status text (not status code) using curl in PHP?
HTTP/1.1 400 Unsupported voice
...
In the above sample HTTP response, I can get the status code with CURLINFO_RESPONSE_CODE
.
But what I am interested in is the status text Unsupported voice
. I couldn't find a CURLINFO constant for status text.
The only option I can think of is to set CURLOPT_HEADER
and parse the response. In case of success response, the response body contains binary data. Hence I am a bit reluctant to use str_*
functions on it.
Is there a better solution?
Upvotes: 3
Views: 253
Reputation: 20654
I have managed to resolve it using CURLOPT_HEADERFUNCTION
. But this does not work with HTTP/2.
<?php
$statusText = null;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://stackoverflow.com/');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$statusText) {
echo $header;
if (is_null($statusText)) {
preg_match('~^HTTP/[^ ]+ \d+ (.*)$~', $header, $match);
$statusText = $match[1];
}
return strlen($header);
});
$response = curl_exec($ch);
echo $statusText;
Upvotes: 1