Search This Blog

Tuesday, April 23, 2013

Persistent/keepalive HTTP with the PHP Curl library? - Stack Overflow

Persistent/keepalive HTTP with the PHP Curl library? - Stack Overflow:


Curl sends the keep-alive header by default, but:
  1. create a context using curl_init() without any parameters.
  2. store the context in a scope where it will survive (not a local var)
  3. use CURLOPT_URL option to pass the url to the context
  4. execute the request using curl_exec()
  5. don't close the connection with curl_close()
very basic example:
function get($url) {
    global $context;
    curl_setopt($context, CURLOPT_URL, $url);
    return curl_exec($context);
}

$context = curl_init();
//multiple calls to get() here
curl_close($context);

No comments:

Post a Comment