检查php中的url是否处于活动状态


Check the url is active or not in php

我需要检查给定的短url是否处于活动状态。并使用以下php函数获取url信息,获取扩展后的url。

get_headers($original_url);

结果是不正确的。我使用当前活动的url对此进行测试。

还有其他办法吗?

提前谢谢。

要检查远程文件是否存在,请使用cUrl

$ch = curl_init('http://example.com/index.php');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
if ($retcode == 200) {
    $isUrlOk = true;
} else {
    $isUrlOk = false;
}
curl_close($ch);