note 129770 added to function.get-headers

From: Date: Wed, 25 Sep 2024 11:17:30 +0000
Subject: note 129770 added to function.get-headers
Groups: php.notes 
Request: Send a blank email to php-notes+get-221553@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/129770 Del: integrated -- https://main.php.net/note/delete/129770/integrated Del: useless -- https://main.php.net/note/delete/129770/useless Del: bad code -- https://main.php.net/note/delete/129770/bad+code Del: spam -- https://main.php.net/note/delete/129770/spam Del: non-english -- https://main.php.net/note/delete/129770/non-english Del: in docs -- https://main.php.net/note/delete/129770/in+docs Del: other reasons-- https://main.php.net/note/delete/129770 Reject -- https://main.php.net/note/reject/129770 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221553) next »