Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查给定的url在php中是否存在?_Php - Fatal编程技术网

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

如何检查给定的url在php中是否存在?,php,Php,我有这样的URL“http://example.com/index.php?id=10”。如何检查此URL是否存在?使用此功能 $url = 'http://www.example.com'; $headers = get_headers($url, 1); if ($headers !== false && substr($headers[0], 9, 3) == 200) { echo 'Page exists'; } 如果网站设置正确,如果URL存在并且

我有这样的URL“http://example.com/index.php?id=10”。如何检查此URL是否存在?

使用此功能

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

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

如果网站设置正确,如果URL存在并且允许您查看,您应该获得
200 OK
状态代码。您可以使用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

代码未测试

您可以卷曲页面或使用file\u get\u contents()函数检查SO中是否存在类似问题。。。!看看这个::只是为了学究。你真的想检查它是否存在吗?或者如果它没有重定向,或者您有权访问它?(它可能存在,但不允许您访问)+1但。。。我想你的意思是“如果URL存在并且你有权访问它(等等)”,200 OK状态码。OP说他想检查它是否存在(如果不允许他访问它,代码将无法正确检查)。你的密码回答了他的问题。我只是想知道这是否真的是他想问的(见)。啊,是的,在回答中澄清了这一点