Home >Backend Development >PHP Tutorial >PHP的snoopy第三方库怎么进行超时判断。

PHP的snoopy第三方库怎么进行超时判断。

WBOY
WBOYOriginal
2016-06-06 20:27:101317browse

因为URL不确定,需要判断url是否存在(是否超时),下面是自己写的

<code>function s($url){
    $snoopy = new Snoopy();
    $snoopy->read_timeout = 5;
    $snoopy->fetch($url);
    if($snoopy->$timed_out === true){
        echo "超时了";
        return;
    }else{
        return $snoopy->response_code;
    }
}
echo s("http://zihonaini.com");</code>

百度半天,也没找到相对应的事例

回复内容:

因为URL不确定,需要判断url是否存在(是否超时),下面是自己写的

<code>function s($url){
    $snoopy = new Snoopy();
    $snoopy->read_timeout = 5;
    $snoopy->fetch($url);
    if($snoopy->$timed_out === true){
        echo "超时了";
        return;
    }else{
        return $snoopy->response_code;
    }
}
echo s("http://zihonaini.com");</code>

百度半天,也没找到相对应的事例

<code>$snoopy->read_timeout = x

update:

你应该关心status code是不是200,而不是失败的各种特列

$snoopy->read_timeout = x;
$snoopy->fetch($url);
if ($snoopy->status == 200) {
  // 成功
} else {
  // 失败
}

或者看这里的例子 http://dret.net/lectures/services-fall06/a/3/README.txt

if ($snoopy->fetch($url)) {
  // 成功
} else {
  // 失败
}

</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn