Jump to content

[SOLVED] Check if URL Exists


Omzy

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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"))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.