【发布时间】: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?