简体   繁体   English

如何在php中检查给定的URL是否存在?

[英]How can i check whether the given url exists or not in php?

I have URL like this " http://example.com/index.php?id=10 ". 我有这样的网址“ http://example.com/index.php?id=10 ”。 How can i check whether this URL exists or not? 如何检查此URL是否存在?

Use the get_headers function. 使用get_headers函数。

$url = 'http://www.example.com';    
$headers = get_headers($url, 1);

if ($headers !== false && substr($headers[0], 9, 3) == 200) {
    echo 'Page exists';
}

If the website is properly set up, you should get a 200 OK status code if the URL exists and you are allowed to see it. 如果网站设置正确,如果URL存在且您被允许查看,则应获得200 OK状态代码。 You can check this with curl: 你可以用curl来检查:

$http = curl_init("http://example.com/index.php?id=10");
curl_exec($http);
$responseCode = curl_getinfo($http, CURLINFO_HTTP_CODE);
if($responseCode == 200)
    //Page exists

Code not tested 代码未经测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM