代码之家  ›  专栏  ›  技术社区  ›  I am the Most Stupid Person

如何找到域是http还是https(有没有www)使用php?

  •  -1
  • I am the Most Stupid Person  · 技术社区  · 6 年前

    我有 百万 (1000000) 领域 名单。

    +----+--------------+--------------------------+
    | Id | Domain_Name  |       Correct_URL        |
    +----+--------------+--------------------------+
    |  1 | example1.com | http://www.example1.com  |
    |  2 | example2.com | https://exmple2.com      |
    |  3 | example3.com | https://www.example3.com |
    |  3 | example4.com | http://example4.com      |
    +----+--------------+--------------------------+
    
    • ID Domain_Name 列已填充。
    • Correct_URL 列为空。

    问题 :我需要填充 校正网址 列。

    我面临的问题是如何在域之前找到前缀部分。也可能是 http:// http://www. https:// https://www.

    如何使用php正确找到上面4中的内容?请注意,我需要对所有1000000个域运行代码….所以我在寻找一种最快的检查方法…

    4 回复  |  直到 6 年前
        1
  •  1
  •   Supun Praneeth    6 年前

    你可以用 cURL 方法:

    $url_list = ['facebook.com','google.com'];
    
    foreach($url_list as $url){
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        curl_exec($ch);
    
        $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
        echo $real_url;//add here your db commands
    
    }
    

    这个需要一些时间,因为它需要最后一个重定向的url。如果你只想检查 http https 你可以试试这个:

    $url_list = ['facebook.com','google.com'];
    
    foreach($url_list as $url){
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_exec($ch);
    
        $real_url =  curl_getinfo($ch, CURLINFO_REDIRECT_URL);
        echo $real_url;//add here your db commands
    
    }
    
        2
  •  6
  •   Ricardo C    6 年前

    除了向每个可能性发出http请求并查看是否得到响应之外,没有其他方法。

    当你断言 “它可以是http:// http://www . 或https://or https://www ,现实世界的域可能提供零、部分或全部或那些(以及各种其他),并且它们可能以ok或重定向或身份验证错误等响应请求。

    http和https不是web应用程序的属性;它们是由端点(web服务器或应用程序防火墙等)处理的通信协议。

    与任何网络通信一样,必须分别探测主机(“在本例中,www”是主机)和端口(不一定,但最常见)80和443。这个探测是一个叫喊,然后你等着看另一边是否有服务在监听。

        3
  •  2
  •   Alexander Holman    6 年前

    给定一个已知的url,您可以使用 get_headers ,从它们中可以确定https是否可用,http是否重定向到https等等。

    详情请参见: http://php.net/manual/en/function.get-headers.php

        4
  •  2
  •   Martin Barker    6 年前

    所以我不得不建立一个类似的系统来验证用户提供的url。

    最后,您需要设置优先级顺序建议的顺序是https over http和www over without,因此您最终得到的优先级列表如下:

    正如其他人所说,您需要使用curl测试这些。

    foreach($domainRows as $domainRow){
        $scheme_list = ['https://www.','https://', 'http://www.', 'http://'];
        $bestUrl = false;
        foreach($scheme_list as $scheme){
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $scheme.$domainRow['Domain_Name']);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
            curl_exec($ch);
    
            $real_url =  curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            if($real_url){
                $bestUrl = $scheme.$domainRow['Domain_Name']
                break;
            }
        }
    
    
        if($bestUrl){
            // you have the best URL to use as $bestUrl save it to your DB Row
        }else{
            // the site is not responding to any URL's do you need to do something here?
        }
    

    }

    或者根据亚历山大·霍尔曼的回答,我完全忘记了 get_headers 你可以做到

    foreach($domainRows as $domainRow){
        $scheme_list = ['https://www.','https://', 'http://www.', 'http://'];
        $bestUrl = false;
        foreach($scheme_list as $scheme){
    
            $res = get_headers($scheme.$domainRow['Domain_Name']);
            // if you want to allow redirects remove/alter this part as it blocks them.
            if($res && isset($res[0])){
                $statusParts = explode(" ", $res[0]);
                if($statusParts[1] == "200"){
                    $bestUrl = $scheme.$domainRow['Domain_Name'];
                    break;
                }
            }
            //end of status check
            //replace with below to allow all responses from server including 404
            /*if($res){
                $bestUrl = $scheme.$domainRow['Domain_Name'];
                break;
            }*/
        }
    
    
        if($bestUrl){
            // you have the best URL to use as $bestUrl save it to your DB Row
        }else{
            // the site is not responding to any URL's do you need to do something here?
        }
    
    }
    

    此代码将按优先级顺序进行测试,与之匹配的第一个代码将停止对其他代码的测试,如果找不到适合它的工作系统,它将告诉您这一点。

    感谢Supun Praneeth,因为我已经采取并扩大了那里的代码,以更好地满足您的需要。