Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting "--keepalive-time" for curl in php

Tags:

php

curl

Some backstory here: https://serverfault.com/questions/714273/curl-returning-error-52-or-56-with-rest-api-call-spanning-more-than-5-minutes

So, I have this REST API call that I need to make via PHP and the only way I can get it to work via the CLI is setting the --keepalive-time for CURL. So how do I do that in PHP? Here is the (censored for creds) working API call via CURL directly:

curl --max-time 600 -k -o dump.txt --connect-timeout 0 --keepalive-time 30 --trace-ascii trace.txt --trace-time -X GET -H "tenant-code: 1cmPx7tqVDVTdN1GSelwycFUmICmASnLCmNQsV72" -H "Authorization: Basic JxHAsXeUiHMRkS8Msiu6pWb3PvY20p6am3QvXCY3knXTAntlxTBS3EyEDgly" -H "Content-Type: application/json" -H "Cache-Control: no-cache" 'https://api.endpoint.com/API/v1/system/users/search?groupid=555'

The --max-time and --connect-timeout values don't seem to matter as much (as long as they are within bounds of what I need) but the --keepalive-time seems to be required to get the data back from the call. Here is some test code I am using:

<?php
$url = "https://api.endpoint.com/API/v1/system/users/search?groupid=555";
$session = curl_init($url);
curl_setopt($session,CURLOPT_RETURNTRANSFER, true);
curl_setopt($session,CURLOPT_TIMEOUT, 600);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_AUTOREFERER, true);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($session, CURLOPT_VERBOSE, true);
curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($session, CURLOPT_NOPROGRESS, false); 
curl_setopt($session, CURLOPT_FORBID_REUSE, true);
curl_setopt($session, CURLOPT_FRESH_CONNECT, true);
$headers = array(
        'tenant-code: 1cmPx7tqVDVTdN1GSelwycFUmICmASnLCmNQsV72',
        'Authorization: Basic JxHAsXeUiHMRkS8Msiu6pWb3PvY20p6am3QvXCY3knXTAntlxTBS3EyEDgly',
        'Cache-Control: no-cache',
        'Accept: application/json');
        curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($session);
$httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);
curl_close($session);
var_dump($httpcode, $output, $session, $headers, $url);
?>

I added curl_setopt($session, CURLOPT_FORBID_REUSE, true); and curl_setopt($session, CURLOPT_FRESH_CONNECT, true); for testing but they don't seem to have any effect on my issue. So what is needed to get this handled by CURL in PHP?

like image 805
Hybrid Avatar asked Aug 14 '15 22:08

Hybrid


1 Answers

If you are using PHP 5.5 or greater (currently 5.5, 5.6, & 7) built with cURL 7.25.0 or greater, you can set these cURL options in PHP to match the --keepalive-time parameter (PHP <= 5.4 did not have these cURL options available):

curl_setopt($session, CURLOPT_TCP_KEEPALIVE, 1);
curl_setopt($session, CURLOPT_TCP_KEEPIDLE, 30);
curl_setopt($session, CURLOPT_TCP_KEEPINTVL, 15);

The CURLOPT_TCP_KEEPIDLE constant in libcurl corresponds to the --keepalive-time command line option for curl.

See the cURL docs regarding CURLOPT_TCP_KEEPALIVE, CURLOPT_TCP_KEEPIDLE, and CURLOPT_TCP_KEEPINTVL for more info.

Note: These options are not available and can't be used if you have PHP 5.4 or lower.

like image 71
drew010 Avatar answered Oct 09 '22 06:10

drew010