PHP中多个页面加载的持久cURL连接

use*_*030 5 php curl pipelining handshake

在页面加载时,我一直在请求来自同一API的数据.有没有办法在多个页面加载之间保持cURL连接活动以减少握手时间?我知道您可以轻松地在同一个PHP进程上使用keep-alive标头发出多个cURL请求,但我希望连接保持活动状态,比如设置一段时间,而不是在进程完成时.

看起来我需要某种守护进程插件才能做到这一点.我对替代解决方案非常开放.它不一定是cURL.我一直在寻找并且没有任何运气.

谢谢!

小智 0

我想说使用pfsockopen()创建持久连接会成功。您可以打开到 HTTP API 服务器的套接字,然后发出多个请求,并在页面加载结束时关闭套接字。

<?php
$host = "protocol://your-api-hostname.com/";
$uri = "/your/api/location";
$port = 80;

// For an HTTP API, for example:
$socket = pfsockopen ($host, $port, $errno, $errstr, 0);
if (!$socket) {
    echo $errno. " - " . $errstr;
} else {
    // You can do here as many requests as you want.
    fputs ($socket, $request1);
    fputs ($socket, $request2);
    // And then keep on reading until the end of the responses
    $i = 0;
    $a = array();
    while (!feof($socket)) {
        $a[$i] = fgets($socket);
        $i++;
    }
fclose($socket);
Run Code Online (Sandbox Code Playgroud)

我不知道这是否正是您所需要的,但它为您提供了比 cURL 更多的选择。