【问题标题】:How to detect whether a website is not overloading or not?如何检测网站是否没有超载?
【发布时间】:2014-12-05 01:23:02
【问题描述】:

我需要找到一种方法来检测网站(joseki 端点)是否过载。 http://128.250.202.125:7001/joseki/oracle 一直在运行,但是当我提交查询时,有时它处于空闲状态。 (即超载,而不是宕机)

到目前为止,我的方法是使用 curl 模拟表单提交。如果 curl_exec 返回 false,我知道网站已超载。

主要问题是我不确定网站超载是否会触发“FALSE 返回”。 我可以使用这个method 记录 curl_exec 的返回,但是这个网站正在关闭。

<?php

$is_run = true;
if($is_run) {
    $url = "http://128.250.202.125:7001/joseki/oracle";

    $the_query = "
PREFIX dc:    <http://purl.org/dc/elements/1.1/>
PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd:   <http://www.w3.org/2001/XMLSchema#>
PREFIX owl:   <http://www.w3.org/2002/07/owl#>
PREFIX fn:    <http://www.w3.org/2005/xpath-functions#>
PREFIX ouext: <http://oracle.com/semtech/jena-adaptor/ext/user-def-function#>
PREFIX oext:  <http://oracle.com/semtech/jena-adaptor/ext/function#>
PREFIX ORACLE_SEM_FS_NS: <http://oracle.com/semtech#timeout=100,qid=123>
SELECT ?sc ?c
WHERE 
  { ?sc rdfs:subClassOf ?c} 
    ";

    // Simulate form submission.    
  $postdata = http_build_query(array('query' => $the_query));
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $tmp_condi = curl_exec($curl);

    // After I submit a simulated form submission, and http://128.250.202.125:7001/joseki/oracle is
    // not responding (i.g. idling), does it definitely returns FALSE????
  if($tmp_condi === FALSE) {
    die('not responding');  
  }
    else {

    }

  curl_close($curl);
}

解决方案

可以通过添加以下内容来解决它,基于此:Setting Curl's Timeout in PHP

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds

【问题讨论】:

  • 你可以在 curl_exec 之后使用curl_error($curl) 来查看是否发生错误。
  • @Asenar 我已经将登录信息放入代码中,我正在等待网站关闭。您是否看到任何网站没有响应,并且 curl_exec 返回 false?

标签: php curl


【解决方案1】:

我需要找到一种方法来检测网站是否响应。 到目前为止,我的方法是使用 curl 模拟表单提交。

我宁愿做 HTTP HEAD 请求 (see docs) 并检查返回码。您不需要返回任何数据,因此无需发送 POST 请求或获取响应。我还为请求设置了缩短超时时间:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);

curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$content = curl_exec($ch);
curl_close($ch);

如果$http_status 为 200(OK),那么远程端也许可以被认为是实时的。

【讨论】:

  • 我为我的问题改写了
  • 对不起,我什至没明白我在问什么。
【解决方案2】:

是的,如果网站在一段时间内没有响应(在CURLOPT_CONNECTIONTIMEOUT 中设置),它将触发一个错误,curl_exec() 将返回false,实际上它也会在任何其他错误上返回false,所以您的代码实际上不会告诉您网站是否已关闭。

【讨论】:

  • 我改写了我的问题。
猜你喜欢
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 2022-12-24
  • 2011-07-23
相关资源
最近更新 更多