Omzy Posted May 24, 2009 Share Posted May 24, 2009 I've got a form which has a field to enter a URL, I've got basic preg_match to see if the URL is in a valid format, but I now want to also check that the URL actually exists on the Internet. And optionally, can this be extended to check that the URL contains XML data, i.e. an RSS feed. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/ Share on other sites More sharing options...
MadTechie Posted May 24, 2009 Share Posted May 24, 2009 why not use $XML = file_get_contents($url); then valid the $XML with $XML = file_get_contents($url); if (preg_match('/^<\?xml/s', $XML)) { echo "valid"; } EDIT: of course this is a simple check but you can expand it to check the XML is a RSS feed Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841160 Share on other sites More sharing options...
Daniel0 Posted May 24, 2009 Share Posted May 24, 2009 If you want to check that the page exist, just check that it returns a 200 status code. Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841161 Share on other sites More sharing options...
Omzy Posted May 24, 2009 Author Share Posted May 24, 2009 what about: fopen($url, "r"); Is that sufficient? Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841164 Share on other sites More sharing options...
MadTechie Posted May 24, 2009 Share Posted May 24, 2009 Yes that would be fine $handle = fopen($url, "r"); if($handle) { $contents = fread($handle, 10); fclose($handle); if (preg_match('/^<\?xml/s', $contents)) { echo "valid"; } } EDIT: as a note i this will only read 10 bytes this is fine for checking its XML (infact 5 is okay) but not for checking its RSS Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841167 Share on other sites More sharing options...
Omzy Posted May 24, 2009 Author Share Posted May 24, 2009 Is there anything quicker than using fopen? It seems to take a long time for some sites and then eventually times out. I need something that will do a very quick check to see if the site is online, perhaps something that checks for header codes? Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841202 Share on other sites More sharing options...
Omzy Posted May 24, 2009 Author Share Posted May 24, 2009 Yes that would be fine $handle = fopen($url, "r"); if($handle) { $contents = fread($handle, 10); fclose($handle); if (preg_match('/^<?xml/s', $contents)) { echo "valid"; } } EDIT: as a note i this will only read 10 bytes this is fine for checking its XML (infact 5 is okay) but not for checking its RSS How can I put all that code in to an ELSE IF statement? Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841214 Share on other sites More sharing options...
MadTechie Posted May 24, 2009 Share Posted May 24, 2009 Try this <?php $curl = curl_init(); $url = 'http://www.idontexist.co.uk'; //No domain $url = 'http://www.google.co.uk'; //no RSS $url = 'http://www.telegraph.co.uk/news/uknews/rss'; //valid $valid = false; curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_TIMEOUT, 5); curl_setopt($curl, CURLOPT_HEADER, TRUE); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($curl); $header = curl_getinfo($curl); if ($header['http_code']=='200') { //echo "Found page<br>\n"; if(stripos($header['content_type'],'text/xml')!==false) { //echo "Valid XML"; $valid = true; } } curl_close($curl); if($valid) { //Blar echo "$url will work"; } ?>?> i have added 3 urls, for an example Note the timeout is set to 5 seconds, if the server is slow then it may fail which a short timeout like that Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841216 Share on other sites More sharing options...
Omzy Posted May 24, 2009 Author Share Posted May 24, 2009 Cheers MadTechie I will also give that a try. Can you see if you can help with my query regarding your earlier code? Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841219 Share on other sites More sharing options...
MadTechie Posted May 24, 2009 Share Posted May 24, 2009 Sure, please note the last code i account for that, (note the last if statement) for the other code a function would probably work best ie <?php if(function urlExists('www.google.co.uk')) { echo "worked"; } function urlExists($url) { $handle = fopen($url, "r"); if($handle) { $contents = fread($handle, 10); fclose($handle); if (preg_match('/^<?xml/s', $contents)) { return true; } } return false; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841222 Share on other sites More sharing options...
Omzy Posted May 24, 2009 Author Share Posted May 24, 2009 Cheers, that doesn't work though, it seems to be allowing invalid documents.. Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841239 Share on other sites More sharing options...
MadTechie Posted May 24, 2009 Share Posted May 24, 2009 lol i'm a dumbarse if(function urlExists('www.google.co.uk')) { echo "worked"; } should be if(urlExists('www.google.co.uk')) { echo "worked"; } Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841243 Share on other sites More sharing options...
Omzy Posted May 24, 2009 Author Share Posted May 24, 2009 LOL I saw that and fixed it. But I think there is a problem in the function, which is why it's not working for me.. Is it possible to assign a variable within an ELSE IF statment, for example: instead of: $handle = fopen($url, "r"); if($handle) something like this: else if($handle=fopen($url, "r")) Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841252 Share on other sites More sharing options...
DarkSuperHero Posted May 24, 2009 Share Posted May 24, 2009 if its a domain name you could use gethostbyname.... http://us3.php.net/manual/en/function.gethostbyname.php Quote Link to comment https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/#findComment-841260 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.