Using php how would I be able to define the variable $type
into the content-type
of http://www.example.com
For example: $type
defined as "text/html"
So far this is what i am working with:
<?php
$url = 'http://www.example.com';
print_r(get_headers($url));
print_r(get_headers($url, 1));
?>
The code may be changed as much as needed
Have you tried:
$type = get_headers($url, 1)["Content-Type"];
As noted in comments by @Michael, this syntax won't work without a very current version of PHP.
Have you also tried:
$headers = get_headers($url, 1);
$type = $headers["Content-Type"];
?
Sometimes get_header return wrong values becouse it read http headers, but not file. It should be better use finfo:
$finfo = new finfo(FILEINFO_MIME_TYPE);
$type = $finfo->buffer(file_get_contents($link));
Not very clear on what you are trying to do, but if you are trying to get the request content type that was sent by the browser to your script, you can do this:
<?php
// Collect all headers
$headers = [];
foreach(getallheaders() as $name => $header){
$headers[strtolower($name)] = $header;
}
// Get the content type header
$contentType = $headers['content-type'];
?>
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.