Page 1 of 1

fopen() doesnot work with http -- Please Help

Posted: Tue Jul 17, 2007 9:30 am
by lekoya
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All,

I have been strugling to get fopen to read a remote http file. It times out,
But strangely it works fine if I replace remote url with local host.

Here is my code:

Code: Select all

function GetPage($WhoIsServerURL) {
//$handle = fopen("$WhoIsServerURL", "rb") or exit("Unable to open file!"); // Exact url
$handle = fopen("http://www.cyberconnect.co.za", "rb") or exit("Unable to open file!"); //hard-coded url for testing
				
			     $contents = '';
			 	while (!feof($handle)) {
			  		$contents .= fread($handle, 8192);
				}
				fclose($handle);
			//	print_r($contents); die();
				return $contents;
}
Is there any thing else that I can try to get over this problem.
Thanks in advance


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jul 17, 2007 9:33 am
by mattcooper
Try using file_get_contents(), works for me every time ;)

fopen() doesnot work with http -- Please Help

Posted: Tue Jul 17, 2007 9:50 am
by lekoya
Thanks for a quick reply, but the system times out on this line :

Code: Select all

$handle = file_get_contents("http://www.google.com") or die("x");
Is there any other way I can make this work.

I get the following error :

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: Connection timed out in /home/tseko/public_html/newjoomla/components/com_domain_register/libwhois.php on line 41

Posted: Tue Jul 17, 2007 7:32 pm
by feyd
Have you tried cURL or Snoopy?

Posted: Tue Jul 17, 2007 10:15 pm
by AKA Panama Jack
Check this...

Code: Select all

$allow_url_fopen = ini_get("allow_url_fopen");
If that returns anything other than 1 then the server has disabled the fopen wrapper for URLs. This means you can't access any url using the fopen function. Localhost is a different matter. :)

You will need to check for CURL and if it is there use it to access a URL. You can check for CURL using...

Code: Select all

$is_curl = function_exists('curl_init');
If it returns 1 then CURL is enabled.

fopen() doesnot work with http -- Please Help

Posted: Wed Jul 18, 2007 2:45 am
by lekoya
Thanks Feyd for suggestions.

I have checked the allow_url_fopen status and it return 1.
But CURL does not return anything?

But still, my code is not working....

I am testing my site on the development environment, and not on production evironment. Can that maybe be a part of the problem?

Any suggestions are welcome.

Thanks

:)

Posted: Wed Jul 18, 2007 3:32 am
by Gente
Have you tried another web sites? http://www.google.com is not general example :wink:
Actually some months ago I also tried to get the content from it with CURL, but no luck.

fopen() doesnot work with http -- Please Help

Posted: Wed Jul 18, 2007 3:57 am
by lekoya
I have tried other websites other than http://www.google.com but still no luck.

I have also tried Snoopy but it also times out.

Any other suggesstions

Posted: Wed Jul 18, 2007 4:08 am
by ocpamit
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


You can use file_get_contents() as mentioned above

Use Curl for better performance

Code: Select all

$chsourcefile = curl_init("http://abc.com/abc.ext"); 
$fp = fopen("/var/www/abc.txt","w+");
curl_setopt ($chsourcefile, CURLOPT_FILE, $fp);
curl_setopt ($chsourcefile, CURLOPT_HEADER, 0);
curl_exec ($chsourcefile);
curl_close ($chsourcefile);	
fclose ($fp);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

fopen() doesnot work with http -- Please Help

Posted: Wed Jul 18, 2007 4:32 am
by lekoya
Thanks for your suggestion.

But I my PHP is not compiled with CURL, so let me try and install CURL and see if that will solve the problem.

In the mean time, any sugesstion is welcome.

Thanks :(


CURL rocks !!. I finally got it working on my one of my production server.

This is how I have done it.

Code: Select all

function GetPage($WhoIsServerURL) {
				
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
	$ch = curl_init();    // initialize curl handle
	curl_setopt($ch, CURLOPT_URL, "$WhoIsServerURL"); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 1);              // Fail on errors
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    // allow redirects
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
	curl_setopt($ch, CURLOPT_PORT, 80);            //Set the port number
	curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
	curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
	$document = curl_exec($ch);
	$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
	'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
	'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
	'@<![\s\S]*?–[ \t\n\r]*>@',         // Strip multi-line comments including CDATA
	'/\s{2,}/',
	
	);
	
	$text = preg_replace($search, "\n", html_entity_decode($document));
	
	$pat[0] = "/^\s+/";
	$pat[2] = "/\s+\$/";
	$rep[0] = "";
	$rep[2] = "";
	
	$text = preg_replace($pat, $rep, 
	
	curl_close($ch);
	//print $text; die();
	return $text;
}

I found the solution from here

Hope this helps someone.

Thanks to everyone who contributed.


:D

Posted: Wed Jul 18, 2007 6:27 am
by volka
Does

Code: Select all

system('ping http://www.google.com');
work?