Page 1 of 1

accessing and verifying a URL

Posted: Sun Apr 09, 2006 6:15 am
by malcolmboston
im using this code to go to a URL and check if its valid

Code: Select all

<?php
function checkValidURL ($URL) {
	$fp = @fsockopen($URL, 80, $errno, $errstr, 30);
	if ($fp) {
		fputs($fp, 'HEAD / HTTP/1.0' . PHP_EOL . 'Host: ' .  $URL . PHP_EOL . PHP_EOL);
		do {
			$sourceCode = fread($fp, 512);
			#echo $sourceCode;
			return TRUE;
		}
		while (strlen($sourceCode));
	} else {
		return FALSE;
	}
}
however it always comes back as false

can someone pls point me in the right direction

Thanks

ps, when i remove the @ from the fsockopen call i get the error

Code: Select all

Warning: fsockopen(): php_network_getaddresses: gethostbyname failed in C:\netserver\www\example.php on line 4

Posted: Sun Apr 09, 2006 8:39 am
by feyd
it was unable to translate the domain name to an IP address.

Posted: Sun Apr 09, 2006 10:23 am
by Oren
Which URL do you supply as the function's argument when you get this error?

Posted: Sun Apr 09, 2006 12:57 pm
by malcolmboston
what actually handles the IP <-> domain name conversion? PHP or the operating system?

my full code including example calls is..

Code: Select all

<?php
function checkValidURL ($URL) {
	$fp = fsockopen($URL, 80, $errno, $errstr, 30);
	if ($fp) {
		fputs($fp, 'HEAD / HTTP/1.0' . PHP_EOL . 'Host: ' .  $URL . PHP_EOL . PHP_EOL);
		do {
			$sourceCode = fread($fp, 512);
			#echo $sourceCode;
			return TRUE;
		}
		while (strlen($sourceCode));
	} else {
		return FALSE;
	}
}

function checkURLForLink ($sourceCode, $linkString) {
	//
}


$array[1] = "http://www.evolution-interactive.co.uk";
$array[] = "http://www.google.co.uk";
$array[] = "http://www.yahoo.co.uk";
$array[] = "http://www.yahoo342345.co.uk";
$array[] = "http://www.digg.com";
$array[] = "http://madeupurlhere.net";
$array[] = "shouldfail";

for ($i = 1; $i <= count($array); $i++)
{
	$isValid = checkValidURL ($array[$i]);
	if ($isValid === TRUE) {
		echo ''.	$array[$i]	.' - valid<br>';
	} else {
		echo ''.	$array[$i]	.' - invalid<br>';
	}
}
?>
the output is..

Code: Select all

<br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to http://www.evolution-interactive.co.uk:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
http://www.evolution-interactive.co.uk - invalid<br><br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to http://www.google.co.uk:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
http://www.google.co.uk - invalid<br><br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to http://www.yahoo.co.uk:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
http://www.yahoo.co.uk - invalid<br><br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to http://www.yahoo342345.co.uk:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
http://www.yahoo342345.co.uk - invalid<br><br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to http://www.digg.com:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
http://www.digg.com - invalid<br><br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to http://madeupurlhere.net:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
http://madeupurlhere.net - invalid<br><br />
<b>Warning</b>:  fsockopen(): php_network_getaddresses: gethostbyname failed in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  fsockopen(): unable to connect to shouldfail:80 in <b>C:\netserver\www\example.php</b> on line <b>3</b><br />
shouldfail - invalid<br>

Posted: Sun Apr 09, 2006 1:38 pm
by feyd
lose the http:// from all of them that's not apart of the connection data.