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

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

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");

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

回复内容:

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

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");

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

$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 {

// 失败

}

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 怪物类Monster的实现: ```cpp class Monster { public: Monster(int hp, int dmg, int def, std::string name) : hitpoint(hp), damage(dmg), defense(def), name(name) {} virtual ~Monster() {} void fight(Monster& other) { while (hitpoint > 0 && other.hitpoint > 0) { // this monster attacks the other one int damage_dealt = this->attack(other); std::cout << this->name << " attacks " << other.name << " and deals " << damage_dealt << " damage." << std::endl; if (other.hitpoint <= 0) break; // the other monster is dead // the other monster attacks this one damage_dealt = other.attack(*this); std::cout << other.name << " attacks " << this->name << " and deals " << damage_dealt << " damage." << std::endl; } std::cout << (hitpoint > 0 ? this->name : other.name) << " wins!" << std::endl; } virtual int attack(Monster& other) { int damage_dealt = damage - other.defense; if (damage_dealt < 1) damage_dealt = 1; other.hitpoint -= damage_dealt; return damage_dealt; } protected: int hitpoint; int damage; int defense; std::string name; }; ``` 2. 猫和狗类的实现: ```cpp class Cat : public Monster { public: Cat(int hp, int dmg, int def, std::string name) : Monster(hp, dmg, def, name) {} virtual int attack(Monster& other) { int damage_dealt = damage * 2 - other.defense; if (damage_dealt < 1) damage_dealt = 1; other.hitpoint -= damage_dealt; return damage_dealt; } }; class Dog : public Monster { public: Dog(int hp, int dmg, int def, std::string name) : Monster(hp, dmg, def, name) {} virtual int attack(Monster& other) { int damage_dealt = (damage - other.defense + 5) * 2; if (damage_dealt < 2) damage_dealt = 2; other.hitpoint -= damage_dealt; return damage_dealt; } }; ``` 使用示例: ```cpp int main() { Cat cat(20, 5, 2, "Tom"); Dog dog(30, 8, 4, "Snoopy"); cat.fight(dog); return 0; } ``` 输出: ``` Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Tom attacks Snoopy and deals 6 damage. Snoopy attacks Tom and deals 16 damage. Snoopy wins! ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值