1

Here is the php.net listing for http_head.

The function prototype is

string http_head ( string $url [, array $options [, array &$info ]] )

A list of $options is here.

I want to use this to validate that a set of URLs is valid.

[url1, url2, url3]

Are there any options that should be set? Is any of the $info relevant to should I just make sure that false is not returned instead of a string?

  • not sure about your question but have you seen this? http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – reikyoushin May 10 '13 at 21:35
  • @reikyoushin: By "valid" he means he wants to make sure the URL actually points somewhere real, and not to a broken location. – Madara's Ghost May 10 '13 at 21:54
  • ahh ic, so he is invoking a get request and whatever is in the header will be validated. correct? – reikyoushin May 10 '13 at 21:57
  • 1
    but how will it deal with valid URLs which currently have a server that is unreacheable? – reikyoushin May 10 '13 at 22:04

1 Answers1

1

You probably don't need to set any of the $options, unless you're behind a proxy server or need to do something unusual. You probably should look through them just in case.

It's not likely you need to mess with $info unless you're debugging, since this gives you more complete visibility into the request and response.

Sample code:

foreach ($urls as $url) {
  $response = http_head($url);
  if ($response !== false) {
    # FIXME do something cool
  } else {
    # FIXME hey that url is broken!
  }
}
Michael Hampton
  • 9,737
  • 4
  • 55
  • 96
  • I've been writing JavaScript, which uses `in` instead of `as`, weird how language designers don't seem to communicate to make common idioms use the same syntax –  May 11 '13 at 15:52