Voting

: six plus one?
(Example: nine)

The Note You're Voting On

rgawenda at gmail dot com
10 months ago
Some notes are using substr to get the response "code" in first ([0]) header after HTTP/1.X (depending on PHP version), but HTTP/2 is nearing a decade in existence and HTTP/3 is widely supported now, so a better, future proof parsing to extract that, according to specs, is:

<?php
$headers
= get_headers($url, true);
$response_code = explode(" ", $headers[0])[1];
?>

Also if you aren't blocking redirects with stream_context_create, take into account that $headers[0] is the response code of the requested URL, while the next resolved and followed redirect is at [1], so you should also do lookup the correct (last) response header first:

<?php
$response_header
= $headers[max(array_filter(array_keys($headers), 'is_int'))];
?>

<< Back to user notes page

To Top