Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get headers does not work with -

Tags:

php

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

like image 686
chillers Avatar asked Jan 23 '13 04:01

chillers


People also ask

Why my header in PHP is not working?

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!

How can I get header content in PHP?

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.

How do I get all headers?

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 .

What does header () do in PHP?

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).


1 Answers

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

like image 178
Matt Avatar answered Oct 06 '22 06:10

Matt