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 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'); ?> 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 ? 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 :'( 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 ? 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 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 ") 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. 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? 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. 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
Archived
This topic is now archived and is closed to further replies.