Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

On one server I have some 30 PHP sites running under Apache. All those sites use the same (HTTP) API to fetch some data. The API is hosted elsewhere (under my control)

The API uses Nginx with keep-alive and the PHP sites use CURL for making the API requests.

A visitor to 1 of the 30 sites would generate an API call, and the connection to the API would be closed by apache/PHP as soon as the HTML is delivered to the visitor.

What I'm looking for is something like a local proxy to the API that is able to maintain the connection to it so the PHP sites can profit from the keepalive.

Anyway of accomplishing this?

share|improve this question
add comment

1 Answer

PHP's socket functions may be the easiest approach. Socket_create will handle IPv4, IPv6, and UNIX connections. A quick example

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
$address = '127.0.0.1';
$port = 80;
socket_connect($socket,$address,$port);
// Sending data
socket_write('Your API commands here');
// Reading data
while ($buffer = socket_read($socket,1024,PHP_NORMAL_READ)) {
  if(trim($buffer) == 'END') {
    break;
  }
}
socket_close($socket);

More examples at php.net: Socket Examples

share|improve this answer
    
Thanks for your answer! But since the sites can generate multiple API requests per second, i don't think a single threaded PHP process running as proxyserver would be a reliable solution.. –  Ivor Oct 12 '12 at 6:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.