fopen() doesnot work with http -- Please Help

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
lekoya
Forum Newbie
Posts: 5
Joined: Mon Jul 16, 2007 7:00 am

fopen() doesnot work with http -- Please Help

Post 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]
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Try using file_get_contents(), works for me every time ;)
lekoya
Forum Newbie
Posts: 5
Joined: Mon Jul 16, 2007 7:00 am

fopen() doesnot work with http -- Please Help

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

Post by feyd »

Have you tried cURL or Snoopy?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
lekoya
Forum Newbie
Posts: 5
Joined: Mon Jul 16, 2007 7:00 am

fopen() doesnot work with http -- Please Help

Post 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

:)
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post 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.
lekoya
Forum Newbie
Posts: 5
Joined: Mon Jul 16, 2007 7:00 am

fopen() doesnot work with http -- Please Help

Post 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
ocpamit
Forum Newbie
Posts: 7
Joined: Fri Jul 13, 2007 6:53 am
Location: Punjab, India

Post 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]
lekoya
Forum Newbie
Posts: 5
Joined: Mon Jul 16, 2007 7:00 am

fopen() doesnot work with http -- Please Help

Post 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
Last edited by lekoya on Wed Jul 18, 2007 8:07 am, edited 2 times in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Does

Code: Select all

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