Apache/PHP - fsockopen() doesn't work (timeout)

The PHP fsockopen function stopped working on one of my webservers and I can't get it to work again. Here's the PHP code I'm trying to use:

<?php
error_reporting(E_ALL);
set_time_limit(90);

echo '<br><br>Opening socket... '; flush();
$fp=fsockopen('212.160.251.2', 80, $errno, $errstr, 15);
stream_set_timeout($fp, 10);
if(!$fp) die('Socket error.');
echo 'OK<br>'; flush();
fputs($fp,"GET /index.php HTTP/1.0\nHost: www.my.link.pl\nContent-type: application/x-www-form-urlencoded\nConnection: Close\nContent-length: 0\n\n");
while (!feof($fp)) echo fread($fp, 8192);
fclose($fp);
?>

Every time I run this stript, I get the following result:

Opening socket...
Fatal error: Maximum execution time of 90 seconds exceeded in /www/test/fsockopen.php on line 6

I don't know what can be wrong. I can easily access the page I want using Linux shell (wget works without problems), but I keep getting timeouts when I try to use the fsockopen function. I also noticed that fopen('http://www...') doesn't work either, in spite of that I have allow_url_fopen turned on.

Any ideas?
LVL 1
pakonetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iamanindianCommented:
Does this work?
-------------------------------------------------------------
<?php
$fp = fsockopen("212.160.251.2", 80, $errno, $errstr, 30);
if (!$fp) {
   echo "$errstr ($errno)<br />\n";
} else {
      fputs($fp,"GET /index.php HTTP/1.0\nHost: www.my.link.pl\nContent-type: application/x-www-form-urlencoded\nConnection: Close\nContent-length: 0\n\n");
      while (!feof($fp)) {
         echo fgets($fp, 128);
      }
      fclose($fp);
}
?>
-------------------------------------------------------------------

Regards
WC
iamanindianCommented:
BTW...
I should have asked the platform details first. What version of Apache/PHP are you running? Apparently...I don't see anything wrong in your code...mine was just a cleaned up version...increased the connecting timeout a bit.
Even without stream_set_timeout I could connect smoothly on IIS5-PHP5.

What is the status at your end?

Regards
WC
pakonetAuthor Commented:
The code is perfectly OK. The problem is that every time I try to call fsockopen it "hangs" and the script is terminated after 90 seconds. My first thought was that port 80 was blocked on the firewall, but wget http://212.160.251.2/index.php works without any problem. I can access MySQL database and use the mail function in PHP, but fsockopen just won't work :(.

The problem may have something to do with the upgrade of Apache to 2.0.54, but I can be wrong. The PHP version is 4.3.7.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

pakonetAuthor Commented:
Still doesn't work :(. Anyone?
iamanindianCommented:
What are your platform details (Windows/Linux)? I ran your script from IIS-PHP5...works ok. I tried from Apache-Linux-PHP4.3.11 & that also works ok.
Is this problem happening when you try to connect to the IP address in particular? Or just any call to fsockopen fails?

WC

pakonetAuthor Commented:
My platform is Linux. All calls to fsockopen fail, regardles of the IP address/host name (I used an IP address here to make sure it's not a DNS issue). It seems like PHP is not able to open sockets at all (even to 127.0.0.1:80).

I did some additional testing, but with no luck :(. BTW the gethostbyaddr() function works okay. Maybe the whole stuff is actually an Apache issue?
iamanindianCommented:
Do you have root access to the server? Perhaps we could try a few options.

WC
iamanindianCommented:
Can we see the output of phpinfo()?

WC

PS: Perhaps a stupid question...but are we running on safe mode?
iamanindianCommented:
Is it possible for you to upgrade PHP version? Perhaps use the same server & run a differenet version of PHP (>4.3.7) to test the same code runs from the same server or not.

Sorry for posting so many times (not at all intention of spamming)..just trying out different avenues.

Regards
WC
pakonetAuthor Commented:
I don't want to post the whole output of phpinfo() here - is there anything specific you want to know? PHP was compiled like this:

./configure --with-mysql --with-zlib --with-ttf --enable-ctype --with-mod_charset --enable-gd-native-ttf --with-freetype-dir=/usr/local --with-jpeg-dir=/usr --with-png-dir=/usr/local --with-gd --with-jpeg --with-png --enable-dbase  --with-dom --with-dom-xslt=/usr/local/ --enable-memory-limit --enable-zend-multibyte  --enable-trackvars --with-apxs2=/usr/local/apache2/bin/apxs

Some configurations options from php.ini:

allow_call_time_pass_reference = On
safe_mode = Off
disable_functions = system, eval, shell_exec, passthru, exec, dl
allow_url_fopen = Off
default_socket_timeout = 60
sockets.use_system_read = On

PHP runs as an Apache module ("Server API: Apache 2.0 Handler").

Anything else you need to know?

pakonetAuthor Commented:
What strikes me is that fsockopen never times out. The script only stops running when the allowed time limit has passed. I wonder if it could actually be the firewall that causes trouble, blocking outbound packets generated by PHP.
iamanindianCommented:
Well...that could surely be a reason. Is it possible to disable the firewall & try once?

WC
iamanindianCommented:
You may also give it a try by running through the command line interface...just a try (part of an attempt to debug)

WC
pakonetAuthor Commented:
Sorry I didn't reply earlier. Fsockopen() works when I run the script through the command line. So it seems to be an Apache issue. Any ideas?
iamanindianCommented:
Yes, it does appear to be an Apache issue...perhaps you should try these:

1. Upgrade PHP & see what happens with the same Apache
or
2. Upgrade Apache & see what happens with same Php

Option 1 might work...(can't blame entirely on Apache...some old Php modules had bugs).

Regards
WC

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pakonetAuthor Commented:
OK, I will try upgrading PHP when I have some spare time. BTW, I have Zend Optimizer installed on the server - could it possibly be messing with fsockopen?
hujiCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: iamanindian

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.