bulldorc Posted May 19, 2007 Share Posted May 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/ Share on other sites More sharing options...
Lumio Posted May 19, 2007 Share Posted May 19, 2007 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257034 Share on other sites More sharing options...
MadTechie Posted May 19, 2007 Share Posted May 19, 2007 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257038 Share on other sites More sharing options...
bulldorc Posted May 19, 2007 Author Share Posted May 19, 2007 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 :'( Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257067 Share on other sites More sharing options...
MadTechie Posted May 19, 2007 Share Posted May 19, 2007 did you try anyones examples ? Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257069 Share on other sites More sharing options...
bulldorc Posted May 19, 2007 Author Share Posted May 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257076 Share on other sites More sharing options...
Lumio Posted May 19, 2007 Share Posted May 19, 2007 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 ") Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257080 Share on other sites More sharing options...
neel_basu Posted May 19, 2007 Share Posted May 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257086 Share on other sites More sharing options...
bulldorc Posted May 19, 2007 Author Share Posted May 19, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257088 Share on other sites More sharing options...
neel_basu Posted May 19, 2007 Share Posted May 19, 2007 And test wheather curl can open any external urls or not. Turn off all types of Firewalls. Quote Link to comment https://forums.phpfreaks.com/topic/52122-need-help-on-file_get_contents/#findComment-257092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.