php - 在 php 中为 curl 设置 "--keepalive-time"

标签 php curl

这里有一些背景故事: https://serverfault.com/questions/714273/curl-returning-error-52-or-56-with-rest-api-call-spanning-more-than-5-minutes

所以,我有这个 REST API 调用,我需要通过 PHP 进行调用,我可以通过 CLI 让它工作的唯一方法是为 CURL 设置 --keepalive-time。那么我如何在 PHP 中做到这一点呢?这是直接通过 CURL 进行的(经过认证的)工作 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_setopt($session, CURLOPT_FORBID_REUSE, true);curl_setopt($session, CURLOPT_FRESH_CONNECT, true); 用于测试,但它们似乎没有对我的问题有任何影响。那么需要什么才能让 PHP 中的 CURL 处理这个问题?

最佳答案

如果您使用的是通过 cURL 7.25.0 或更高版本构建的 PHP 5.5 或更高版本(目前为 5.5、5.6 和 7),您可以在 PHP 中设置这些 cURL 选项以匹配 --keepalive-time 参数(PHP <= 5.4 没有可用的 cURL 选项):

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

libcurl 中的CURLOPT_TCP_KEEPIDLE 常量对应于curl--keepalive-time 命令行选项。

请参阅有关 CURLOPT_TCP_KEEPALIVE 的 cURL 文档, CURLOPT_TCP_KEEPIDLE , 和 CURLOPT_TCP_KEEPINTVL了解更多信息。

注意:如果您使用的是 PHP 5.4 或更低版本,则这些选项不可用且无法使用。

关于php - 在 php 中为 curl 设置 "--keepalive-time",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32019448/

相关文章:

PHP 日期时间与 MySQL 日期时间

php - 如何使用php在linux机器上加入mp3

php - 如何使用 PHP 检测 CP437

php - .htaccess 从站点根目录重定向到公用文件夹,在 URL 中隐藏 "public"?

php - 如何获取下载链接的 URL

elasticsearch - CURL:不支持Content-Type header [application/x-www-form-urlencoded]

java - 将带有图像的 Python 请求转换为 cURL

javascript - 将 json api curl 从 javascript 重写为 php

php: 自动缩进整个代码?

php - curl 和调整远程图像的大小