Ping网站并用PHP返回结果

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


救命 :)


慕标5832272
浏览 629回答 3
3回答

狐的传说

function urlExists($url=NULL)&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; &nbsp; if($url == NULL) return false;&nbsp;&nbsp;&nbsp; &nbsp; $ch = curl_init($url);&nbsp;&nbsp;&nbsp; &nbsp; curl_setopt($ch, CURLOPT_TIMEOUT, 5);&nbsp;&nbsp;&nbsp; &nbsp; curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);&nbsp;&nbsp;&nbsp; &nbsp; curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);&nbsp;&nbsp;&nbsp; &nbsp; $data = curl_exec($ch);&nbsp;&nbsp;&nbsp; &nbsp; $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);&nbsp;&nbsp;&nbsp; &nbsp; curl_close($ch);&nbsp;&nbsp;&nbsp; &nbsp; if($httpcode>=200 && $httpcode<300){&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp;&nbsp;&nbsp; &nbsp; } else {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp;}&nbsp;&nbsp;从这篇文章中获取了有关如何检查URL是否存在的信息。因为Twitter在维护时应该提供300以上的错误消息或404,所以应该可以正常工作。
打开App,查看更多内容
随时随地看视频慕课网APP