curl - PHP fsockopen connect timeout via browser but not via CLI -
i'm fighting bizarre problem , running out of ideas, hope here me out. it's random connection timeout external server - frequent when ran browser, rare when ran cli. more details , code:
on site example.com
have form. once user submits (http post), php sends request (another post) external server http://external.com/check.php
post-data. once gets response, proceeds execution of rest of code , outputs web page in end. looks this:
if (!empty($_post) { // here goes post validation stuff, , then: req = http_build_query($_post); $http_request = "post /check.php http/1.0\r\n"; $http_request .= "host: external.com\r\n"; $http_request .= "content-type: application/x-www-form-urlencoded;\r\n"; $http_request .= "content-length: " . strlen($req) . "\r\n"; $http_request .= "user-agent: speciallydefinedforexternal.com/php\r\n"; $http_request .= "\r\n"; $http_request .= $req; } if (false == ($fs = fsockopen($host, $port, $errno, $errstr, 10))) { throw new exception($errstr, $errno); } fwrite($fs, $http_request); while (!feof($fs)) { $response .= fgets($fs, 1024); // 1 tcp-ip packet } fclose($fs); // stuff depending on on $response
everything works fine when run on development server, once deploy production, randomly keeps throwing exceptions @ fsockopen()
call.
i checked in log files 15% of fsockopen()
calls successful, , rest 110 connection timed out
errors.
the weirdest thing is, when log in production server , run same snippet of code via cli, 0.1% of requests face connection timeout error, , 99.9% work perfect! if put in in loop of 2000 iterations, ratio stays same.
i talked administrators of external.com
, said have 5% server load, , other customers not face timeout problems, looks php or network issue.
configuration of development server , production server identical, difference load - 150 requests per second in production. clear: these 150 requests visiting users, not each of them submits form , opens socket. there 14 such users per second, i.e. there 14 posts per second sent external.com
server.
when tried replace fsockopen()
curl
, got better - 45% of posts sent external.com
successful (via browser) - still unsatisfactory. when run cli - works perfect. , error message different, means same: couldn't connect host
.
any ideas reason, requests via browser tend fail, while sent via cli work fine?
if matters: i'm using php 5.3.27 , webserver apache. version number identical when call phpinfo()
via browser , php -v
via cli.
thanks in advance,
pstryk
* edit *
it turned out problem in router configuration.
administrators of external.com
didn't see load increase, because looking @ wrong machine. culprit their router, not server external.com
. have changed in router configuration, refrain disclosing setting exactly, guess we'll never find out. funny thing is, since have fixed issue, same problem appears on development server... that's can live with. clear: on side, development server uses different router production server, configuration same (except ips , on). thank tried me out issue. apparently there nothing do.
maybe you've checked configuration identical php-cli, mod_php (dev, stage, live)? seems there short timeout configured. explain different behavior of fsockopen , curl, have different settings iirc.
please have at: http://php.net/stream_set_timeout in documentation of fsockopen noticed timeout opening socket.
Comments
Post a Comment