Page 1 of 1

Warning: fopen(): php_network_getaddresses

Posted: Fri Jan 21, 2005 4:53 pm
by hm2k

Code: Select all

$xurl = "http://www.".$domain."/";
	$handle = fopen($xurl, "r");
	$xhtml = '';
	while (!feof($handle)) {
	  $xhtml .= fread($handle, 1024);
	}
	fclose($handle);
When I use this to lookup a domain that does not resolve, I get lots of errors such as:
Warning: fopen(): php_network_getaddresses: gethostbyname failed
Warning: fopen(http://www.superman.co.uk/): failed to open stream: No error
How can I avoid this problem?

Posted: Fri Jan 21, 2005 4:56 pm
by feyd
use the error control operators.

Posted: Fri Jan 21, 2005 5:01 pm
by hm2k
I read http://us2.php.net/operators.errorcontrol however I still don't quite understand how to avoid this.

Posted: Fri Jan 21, 2005 5:09 pm
by hm2k
I have slighly modified the script:

Code: Select all

$xurl = "http://www.".$domain."/";
	$handle = @fopen($xurl, "r");
	if (error_reporting() == 0) return;
	$xhtml = '';
	while (!feof($handle)) {
	  $xhtml .= fread($handle, 1024);
	}
	fclose($handle);
However I am still getting errors:
Warning: feof(): supplied argument is not a valid stream resource in ~\x.php on line 49

Warning: fread(): supplied argument is not a valid stream resource in ~\x.php on line 50

Posted: Fri Jan 21, 2005 5:10 pm
by feyd

Code: Select all

<?php

	error_reporting(E_ALL);
	ini_set('display_errors','1');

	$domain = 'superman.co.uk';
	$xurl = "http://www.".$domain."/";
	$handle = @fopen($xurl, "r");
	$xhtml = '';
	
	if($handle !== false)
	&#123;
		while (!feof($handle)) &#123;
			$xhtml .= fread($handle, 1024);
		&#125;

		fclose($handle);
	&#125;
	else
	&#123;
		$xhtml = 'failed to open page';
	&#125;
	
	echo $xhtml;

?>

Code: Select all

failed to open page
sure works.. :roll: