note 129772 added to function.get-headers
| From: | rgawenda@gmail.com | Date: | Wed, 25 Sep 2024 14:52:09 +0000 |
| Subject: | note 129772 added to function.get-headers | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221555@lists.php.net to get a copy of this message | ||
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'))];
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 80.91.90.170)
----
Manual Page -- https://php.net/manual/en/function.get-headers.php
Edit -- https://main.php.net/note/edit/129772
Del: integrated -- https://main.php.net/note/delete/129772/integrated
Del: useless -- https://main.php.net/note/delete/129772/useless
Del: bad code -- https://main.php.net/note/delete/129772/bad+code
Del: spam -- https://main.php.net/note/delete/129772/spam
Del: non-english -- https://main.php.net/note/delete/129772/non-english
Del: in docs -- https://main.php.net/note/delete/129772/in+docs
Del: other reasons-- https://main.php.net/note/delete/129772
Reject -- https://main.php.net/note/reject/129772
Search -- https://main.php.net/manage/user-notes.php