Para que funcione em anteriores ao 5.4, como php5.3 e php5.3, pode-se criar urls "reservadas" usando ErrorDocument
(apache, veja as alternativas pra outros servidores abaixo), usando por exemplo:
.htacces (apache)
ErrorDocument 403 /index.php/RESERVED.HTTP-STATUS-403.html
ErrorDocument 404 /index.php/RESERVED.HTTP-STATUS-404.html
nginx
error_page 404 /RESERVED.HTTP-STATUS-404.html;
error_page 403 /RESERVED.HTTP-STATUS-403.html;
location ~ ^/RESERVED\.HTTP\-STATUS\-(403|404)\.html$ {
rewrite ^/RESERVED\.HTTP\-STATUS\-(403|404)\.html$ /index.php$0 last;
}
IIS
<httpErrors errorMode="Custom">
<remove statusCode="403" />
<remove statusCode="404" />
<error statusCode="403" path="/index.php/RESERVED.HTTP-STATUS-403.html" responseMode="ExecuteURL" />
<error statusCode="404" path="/index.php/RESERVED.HTTP-STATUS-501.html" responseMode="ExecuteURL" />
</httpErrors>
PHP
O arquivo deve conter este código:
/*Verifica se a função não esta disponível (versões anteriores ao php5.4)*/
if (false === function_exists('http_response_code')) {
/*Fallback para versões mais antigas que o PHP5.4*/
function http_response_code($code = null)
{
static $currentStatus;
if ($code === null) {
if ($currentStatus !== null) {
return $currentStatus;
}
$currentStatus = 200;
if (empty($_SERVER['PHP_SELF']) === false &&
preg_match('#/RESERVED\.HTTP\-STATUS\-(\d{3})\.html$#', $_SERVER['PHP_SELF'], $match) > 0)
{
$currentStatus = (int) $match[1];
}
} elseif (is_int($code) && headers_sent() === false) {
header('X-PHP-Response-Code: ' . $code, true, $code);
$currentStatus = $code;
}
return $currentStatus;
}
}
Note que a função sempre irá disparar para o index.php, mas você pode trocar por error.php ou ao seu gosto.
curl_getinfo
usando como argumentoCURLINFO_HTTP_CODE
? ela está disponível desde o PHP 4.0.4. Na tua primeira frase tem uma afirmação que contradiz o título. =)<?php http_response_code(404); echo http_response_code();
. Isto não funciona<?php echo http_response_code();
CURLOPT_NOBODY
para requisitar somente o cabeçalho http e não o conteúdo todo, assim irá diminuir o tempo para obter a resposta.