Link to home
Start Free TrialLog in
Avatar of R7AF
R7AFFlag for Netherlands

asked on

url_exists returns false for existing url

I use the function url_exists() to check whether a url exists (wow). For one set of urls, this doesn't work.

- For the same domain but different urls, the functions works correctly.
- When I download the xml file and save it locally, and run the function using the local url it works.

The headers I get back are as follows:

[0] => HTTP/1.1 200 OK
[1] => Date: Wed, 20 May 2009 15:26:22 GMT
[2] => Server: Apache/2.2.8 (Win32) mod_ssl/2.2.8 OpenSSL/0.9.8k
[3] => Last-Modified: Wed, 20 May 2009 15:21:54 GMT
[4] => ETag: "12d00000000006c-2635-46a59965b054e"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 9781
[7] => Connection: close
[8] => Content-Type: application/xml

Any ideas what could cause this problem?
<?php
function url_exists($url) 
{
   $hdrs = get_headers($url);
    return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
} 
?>

Open in new window

Avatar of jessc7
jessc7
Flag of United States of America image

I'm not sure all the escaping is necessary, is it? The following works for me using an online expression checker:

^HTTP\/\d+\.\d+\s+2\d\d\s+.*$

http://www.nvcc.edu/home/drodgers/ceu/resources/test_regexp.asp

Here is an alternative using CURL:
http://www.ajaxapp.com/2009/03/23/to-validate-if-an-url-exists-use-php-curl/
Here are two screenshots of the regex. I'm still thinking through why your original works sometimes...
original-fail.png
succeed.png
ASKER CERTIFIED SOLUTION
Avatar of jessc7
jessc7
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Screenshot of validation results.
new1.png
Avatar of R7AF

ASKER

Thank you! I will test this on Friday and let you know if it solved my problem.
Hi R7AF, were you able to complete further testing on this issue?
Avatar of R7AF

ASKER

Yes, and the issue still exists. I'll try to update this question soon.
Avatar of R7AF

ASKER

Thanks for your input. I'm closing this question now. Maybe I'll look into it in the future again, and will post another question about it then.