Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置“--“保持生命时间”;用于php中的curl_Php_Curl - Fatal编程技术网

设置“--“保持生命时间”;用于php中的curl

设置“--“保持生命时间”;用于php中的curl,php,curl,Php,Curl,这里有一些背景故事: 因此,我有一个RESTAPI调用,我需要通过PHP进行调用,通过CLI使其工作的唯一方法是为CURL设置--keepalive time。那么如何在PHP中实现这一点呢?下面是通过CURL直接进行的(针对creds进行的删失)工作API调用: curl --max-time 600 -k -o dump.txt --connect-timeout 0 --keepalive-time 30 --trace-ascii trace.txt --trace-time -X G

这里有一些背景故事:

因此,我有一个RESTAPI调用,我需要通过PHP进行调用,通过CLI使其工作的唯一方法是为CURL设置
--keepalive time
。那么如何在PHP中实现这一点呢?下面是通过CURL直接进行的(针对creds进行的删失)工作API调用:

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'
--max time
--connect timeout
值似乎没有那么重要(只要它们在我需要的范围内),但是从调用中获取数据似乎需要
--keepalive time
。以下是我正在使用的一些测试代码:

<?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);
?>


我添加了
curl\u setopt($session,CURLOPT\u禁止重用,true)
curl\u setopt($session,CURLOPT\u FRESH\u CONNECT,true)用于测试,但它们似乎对我的问题没有任何影响。那么,用PHP中的CURL处理这个问题需要什么呢?

如果您使用的是用CURL 7.25.0或更高版本构建的PHP5.5或更高版本(目前为5.5、5.6和7),那么您可以在PHP中设置这些CURL选项,以匹配
--keepalive time
参数(PHP我将CURL升级到
7.44.0
,并将PHP设置为使用该版本。我将该代码添加到测试代码中并运行了它。5分钟后仍然失败。我没有收到任何错误,因此我假设它可以工作,但无论出于何种原因,它都不能解决我的问题。我重新检查了CURL源代码,它传递了
--keepalive time
参数ts
CURLOPT_TCP_KEEPIDLE
CURLOPT_TCP_KEEPINTVL
在底层调用中,因此我查看了PHP源代码,结果发现如果PHP不识别curl选项(这里没有)然后它不会在底层句柄上调用
curl\u easy\u setopt
,因此这些选项被忽略。所以没有办法让curl/PHP设置该参数?没有,但我再次检查,这些选项似乎在PHP5.5、5.6和7alpha中可用,所以如果您能够升级到PHP5.5,那么您可以使用我添加的
定义的这些选项('CURLOPT_TCP_KEEPALIVE')定义('CURLOPT_TCP_KEEPALIVE',213);定义('CURLOPT_TCP_keepile'))|定义('CURLOPT_TCP_keepile',214)
curl_setopt($session,CURLOPT_TCP_KEEPALIVE,1);curl_setopt($session,CURLOPT_TCP_keepile,30);
到我的测试代码并升级到PHP5.5.28,我仍然看到相同的问题。我需要对代码进行更改还是系统上存在其他问题?CURL是7.44.0。
curl_setopt($session, CURLOPT_TCP_KEEPALIVE, 1);
curl_setopt($session, CURLOPT_TCP_KEEPIDLE, 30);
curl_setopt($session, CURLOPT_TCP_KEEPINTVL, 15);