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
https://forums.phpfreaks.com/topic/159462-solved-check-if-url-exists/
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

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?

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

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.