【问题标题】:HTTP Get time for responseHTTP 获取响应时间
【发布时间】:2020-12-13 23:35:38
【问题描述】:

我正在玩 guzzle 并尝试构建一个简单的页面,我可以将我的域添加到 - 并让它检查它们当前是否在线/可访问。

我目前可以检查域的数组/列表是否在线,或者是否由于某种原因被拒绝。我还希望能够在我的日志/数据库中看到从我向 [mydomain.com] 发送 HTTP 请求到响应返回需要多长时间。

当前代码:

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

if(!empty($aDomains))
{       
    $oClient            = new Client(['expect' => false]);
    $aAcceptedResponse  = [];
    $aRejectedResponses = [];
    $aCreatedRequests    = [];
    

    foreach ($aDomains as $iDomainKey => $sDomain) 
    {          
        array_push($aCreatedRequests, new Request('GET', $sDomain));
    }

    $pool = new Pool($oClient, $aCreatedRequests, 
    [
        'concurrency' => 50,
        'options' => ['timeout' => 10],
        'fulfilled'   => function ($response, $index) use (&$aAcceptedResponse) 
        {
            $aAcceptedResponse[] = $index;
        },
        'rejected'    => function ($reason, $index) use(&$aRejectedResponses)  
        {
            $aRejectedResponses[] = $index;
        },
    ]);

    $promise = $pool->promise();
    $promise->wait();
}  

我想我可以在 guzzle 响应对象中找到一些东西,但到目前为止我什么都找不到 - 我是盲人还是看不到这个?

【问题讨论】:

  • 响应不能包含此类信息,因为它不知道发送请求的确切时间。对方只知道它何时收到请求。您应该手动测量。 This 应该让你开始。
  • 在文档中查找 on_stats,它有一个函数getTransferTime() 可以让您有时间在文档中查看

标签: php get guzzle


【解决方案1】:

感谢 El_Vanja 的回答,我只使用全局时间戳就知道了:

    $iStartExecutionTime = microtime(true);
    $oClient             = new Client(['expect' => false]);
    $aAcceptedResponse   = [];
    $aRejectedResponses  = [];
    $aCreatedRequests    = [];      

    foreach ($aDomains as $iDomainKey => $oDomain) 
    {          
        array_push($aCreatedRequests, new Request('GET', $oDomain->sDomainName));
        update_domain_uptime_monitor($oDomain->iID, 1, date('Y-m-d H:i:s', strtotime('NOW')+$oDomain->iInterval), date('Y-m-d H:i:s', strtotime('NOW')));
    }

    $pool = new Pool($oClient, $aCreatedRequests, 
    [
        'concurrency' => 50,
        'options' => ['timeout' => 10],
        'fulfilled'   => function ($response, $index) use (&$aAcceptedResponse) 
        {               
            $aAcceptedResponse[$index] = (microtime(true)-$GLOBALS['iStartExecutionTime']);
        },
        'rejected'    => function ($reason, $index) use(&$aRejectedResponses)  
        {
            $aRejectedResponses[] = $index;
        },
    ]);

    $promise = $pool->promise();
    $promise->wait();
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-28
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多