Posted By

ginoplusio on 01/06/10


exists curlphpgetheadersurl checkerurl

Versions (?)

Who likes this?

3 people have marked this snippet as a favorite


Url exists function to check remote url

 / Published in: PHP


If you have to test if a remote url is correct you can use CURL and get the headers returned by the http request. If you receive a 200 code, than it’s ok, else the url is not correct:

  1. function url_exists($url) {
  2. $ch = @curl_init($url);
  3. @curl_setopt($ch, CURLOPT_HEADER, TRUE);
  4. @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
  5. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  6. @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  7. $status = array();
  8. preg_match('/HTTP\/.* ([0-9]+) .*/', @curl_exec($ch) , $status);
  9. return ($status[1] == 200);
  10. }

Report this snippet  

You need to login to post a comment.