R7AF
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-46a5 9965b054e"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 9781
[7] => Connection: close
[8] => Content-Type: application/xml
Any ideas what could cause this problem?
- 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-46a5
[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;
}
?>
Here are two screenshots of the regex. I'm still thinking through why your original works sometimes...
original-fail.png
succeed.png
original-fail.png
succeed.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Screenshot of validation results.
new1.png
new1.png
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?
ASKER
Yes, and the issue still exists. I'll try to update this question soon.
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.
^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/