So i want to echo only a header not all of them.
An example the Server header which contains the information what server it is.
Or X-powered: PHP/version header.
I know i can use print_r($headers[“0”]); but the problem here is i will don’t know what number is that header exactly.
Array ( [0] => HTTP/1.1 403 Forbidden [1] => Date: Fri, 26 Jun 2020 21:44:53 GMT [2] => Content-Type: text/html; charset=UTF-8 [3] => Connection: close [4] => CF-Chl-Bypass: 1 [5] => Set-Cookie: __cfduid=d3cb45768070a21a77835f417592827541593207893; expires=Sun, 26-Jul-20 21:44:53 GMT; path=/; domain=.onetap.com; HttpOnly; SameSite=Lax...................
how i can print only the header i want without knowing the array number.
$url = "https://www.google.com/"; $headers = get_header($url); print_r($headers["0"]);
Advertisement
Answer
Iterate over your $headers
and search for a substring in every header:
$headers = get_headers($url); foreach ($headers as $header) { if (false !== strpos($header, 'X-powered:')) { echo $header; // now extract required value, e.g: $data = explode('/', $header); echo 'Version is: ' . $data[1]; // use break to stop further looping break; } }
Using second parameter of get_headers
as 1, will even give you an array, so you can check if required key exists with isset
or array_key_exists
:
$headers = get_headers($url); if (isset($headers['X-powered'])) { echo $headers['X-powered']; // now extract required value, e.g: $data = explode('/', $headers['X-powered']); echo 'Version is: ' . $data[1]; }