Jump to content

Recommended Posts

Hi

 

I have used the function file_get_contents to retrieve html content from a website. My code works fine in localhost but after uploading it to the server, the following error appears:

 

Warning: file_get_contents(http://someurl) [function.file-get-contents]: failed to open stream: Connection refused in /dirc/code.php on line 13

 

Any expert advice is appreciated.

 

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/
Share on other sites

What do you want to do?

If you only want to read a file of you server try this:

<?php
function open_file($file) {
	if (!file_exists($file)) return false;
	if (($filesize = filesize($file)) == 0) return '';
	$fp = fopen ($file, 'r');
	$content = fread($fp, $filesize);
	fclose($fp);
	return $content;
}

echo open_file('./index.php');
?>

Thanks for your quick responses.

 

My code is as simple as this:

 

$url = "http://www.google.com";
$source = file_get_contents($url);
echo htmlspecialchars($source);

 

It sure does work in localhost but on my new server it gets warning error:

 

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: Connection refused in /testing/code.php on line 13

 

I did googling for a while and some suggest that my server may have firewall setting problems.

 

Specific solution is really needed.

 

I am waiting to thank someone from now  :'(

LOL again..

 

Try

ini_set('default_socket_timeout',    120);   
$content=file_get_contents("http://www.google.com",FALSE,NULL,0,20);

 

anything special about the URL you are trying to connect to ?

 

Thanks but sorry it does not work. The error is hanging at:

 

Warning: file_get_contents(http://www.google.com) [function.file-get-contents]: failed to open stream: Connection refused in /testing/code.php on line 13

Try this:

<?php
$handle = fopen("http://www.google.com/", 'are');
while (($part = fgets($handle)) !== false) {
	echo $part;
}
fclose($handle);
?>

 

something doesn't function here... after "http://www.google.com" is not 'are' but only _r (without the underscore _) (in ")

Some servers wouldn't let you to connect with external hosts to prevent abuse and spam . and I think you cant also use curl to open an external site.

As I am using sourceforge.net I know that they wouldn't let you to do that.

 

The server is mine. So do you know what I should work on the server to get external contents?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.