accessing and verifying a URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

accessing and verifying a URL

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it was unable to translate the domain name to an IP address.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Which URL do you supply as the function's argument when you get this error?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

lose the http:// from all of them that's not apart of the connection data.
Post Reply