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"]);
Use second parameter
print_r(get_headers($url, 1));
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)
So you can access specific header.
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];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.