简体   繁体   English

检查网址是否存在php

[英]check if url exists php

I'm currently using the following method to check if a url exists 我目前正在使用以下方法检查网址是否存在

$url = 'https://www.facebook.com/a-test-example-232397848665383511';
$headers = @get_headers($url);
if(strpos($headers[0],'200')===false){
    print('NOT found!');
} else {
    print('found!');
}

This prints NOT found! NOT found!此打印NOT found! even though the page clearly resolves when visited. 即使该页面在访问时清晰可见。 I print the headers and found it is because it returns a 302 . 我打印标题,发现它是因为它返回302 Is there a way of doing a strpos to test for all possible header values that resolve? 有没有做的方式strpos来测试该解决所有可能的标头值?

Current output of headers: 标题的当前输出:

Array
(
    [0] => HTTP/1.1 302 Found
    [1] => Location: https://www.facebook.com/unsupportedbrowser
    [2] => Vary: Accept-Encoding
    [3] => Content-Type: text/html
    // more array items

If I type in a url that i know fails I get the following: 如果输入我知道失败的网址,则会得到以下信息:

Array
(
    [0] => HTTP/1.1 404 Not Found
    [1] => P3P: CP="Facebook does not have a P3P policy." 
    [2] => Strict-Transport-Security: max-age=15552000; preload
    // rest of array

Is it safe to test simply for a 404? 仅针对404测试是否安全?

I would use cURL for url verification. 我会使用cURL进行网址验证。 An example method would be as follows 一个示例方法如下

    public function urlExists($url) {

        $handle = curl_init($url);
        curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

        $response = curl_exec($handle);
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

        if($httpCode >= 200 && $httpCode <= 400) {
            return true;
        } else {
            return false;
        }

        curl_close($handle);
    }

Server can respond with different status codes as described in RFC 2616 For you task all codes 2xx and 3xx mean success. 服务器可以使用RFC 2616中描述的不同状态代码进行响应。对于您来说,所有代码2xx和3xx均表示成功。

Performance note: get_headers by default uses GET method but if you not interested in page content it's better and faster to use HEAD method. 性能说明:默认情况下,get_headers使用GET方法,但是如果您对页面内容不感兴趣,最好使用HEAD方法。

stream_context_set_default(
  array(
      'http' => array(
          'method' => 'HEAD'
      )
  )
);
$headers = @get_headers($url);
$status = substr($headers[0], 9, 3);
if ($status >= 200 && $status < 400 ) {
  print('found!');
} else {
  print('NOT found!');
}

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

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