Posted By

ginoplusio on 01/06/10


Tagged

exists curlphpgetheadersurl checkerurl


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

netzwerktourist
ginoplusio
redstorm


Url exists function to check remote url


 / Published in: PHP
 

URL: http://www.barattalo.it/2010/01/06/test-if-a-remote-url-exists-with-php-and-curl/

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.