I'm trying to get the output of URL headers and it works great with a subdomain example
subdomain.example.com
I will get the normal message HTTP 200 ok etc, but if the subdomain has -
in it nothing is displayed in the headers.
-test.tumblr.com
or test-.tumblr.com
Is there a better way of doing this? My example code is below.
$url = "http://-test.tumblr.com";
$url_headers = @get_headers($url);
$urlheader = $url_headers[0];
echo $urlheader;
If it cannot be done with checking the headers how else would I go about seeing if the page exists. I have tried using curl but it does the same.
Thanks
Solution to the Problem To solve this problem, we have to store the header in a buffer and send the buffer at the end of the script, so to store the header in the buffer, we will use the php ob_start() function and to clean the buffer, we will use the ob_end_flush() function. See the below code snippet. This is it!
The get_headers() function in PHP is used to fetch all the headers sent by the server in the response of an HTTP request. Parameters: This function accepts three parameters as mentioned above and described below: $url: It is a mandatory parameter of type string. It defines the target URL.
you can use getallheaders() to get an array of all HTTP headers sent. Show activity on this post. Every HTTP request header field is in $_SERVER (except Cookie ) and the key begins with HTTP_ . If you're using Apache, you can also try apache_request_headers .
The header() function in PHP sends a raw HTTP header to a client or browser. Before HTML, XML, JSON, or other output is given to a browser or client, the server sends raw data as header information with the request (particularly HTTP Request).
When you un-supress the warning from get_headers do you get the warning that you can only use get_headers on urls?
Warning: get_headers(): This function may only be used against URLs
try adding http:// in front of the subdomain. I added a subdomain test-.myserver.loc to my hosts file and was able to get the headers.
$url = 'http://test-.myserver.loc';
print_r(get_headers($url))
http://us3.php.net/get_headers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With