6

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

chillers
  • 1,447
  • 2
  • 15
  • 25

1 Answers1

14

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

Matt
  • 2,341
  • 4
  • 20
  • 20
  • 1
    the address also has to resolve... before I added it to my hosts file, I go this error: Warning: get_headers(): php_network_getaddresses: getaddrinfo failed: Name or service not known – Matt Jan 23 '13 at 04:45
  • I added a comment to my question that shows the url as a example I'm trying to get. Thanks – chillers Jan 23 '13 at 04:47
  • 1
    I basically want to see if the page exist/real using the url in my comments. – chillers Jan 23 '13 at 04:49
  • var_dump(gethostbynamel('http://-blue--crush-.tumblr.com')); will return false if doesn't exist http://php.net/manual/en/function.gethostbynamel.php – Matt Jan 23 '13 at 04:54
  • I'm getting false on real pages and non real pages. – chillers Jan 23 '13 at 04:59
  • I think you are on the right track... have you seen this? http://stackoverflow.com/questions/2280394/check-if-an-url-exists-in-php – Matt Jan 23 '13 at 05:25
  • I tried curl already it won't fetch anything when `-` is at the end or beginning of the sub domain. I'm actively looking for a solution. Thanks – chillers Jan 23 '13 at 05:33