Ping网站并在PHP中返回结果 [英] Ping site and return result in PHP

查看:79
本文介绍了Ping网站并在PHP中返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小的IF过程,该过程将检查Twitter是否可用(例如,与现在不同),并将返回true或false.

I'd like to create a small IF procedure that will check if Twitter is available (unlike now, for example), and will return true or false.

帮助:)

推荐答案

function urlExists($url=NULL)  
{  
    if($url == NULL) return false;  
    $ch = curl_init($url);  
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $data = curl_exec($ch);  
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    curl_close($ch);  
    if($httpcode>=200 && $httpcode<300){  
        return true;  
    } else {  
        return false;  
    }  
}  

此内容是从关于如何检查网址是否存在的这篇文章.由于Twitter在维护时应该提供300以上的错误消息或404,因此应该可以正常工作.

This was grabbed from this post on how to check if a URL exists. Because Twitter should provide an error message above 300 when it is in maintenance, or a 404, this should work perfectly.

这篇关于Ping网站并在PHP中返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆