如何使用PHP查找域是HTTP还是HTTPS(带或不带WWW)?
我有1000000个(1000000个)域名列表如何使用PHP查找域是HTTP还是HTTPS(带或不带WWW)?,php,http,url,https,domain-name,Php,Http,Url,Https,Domain Name,我有1000000个(1000000个)域名列表 +----+--------------+--------------------------+ | Id | Domain_Name | Correct_URL | +----+--------------+--------------------------+ | 1 | example1.com | http://www.example1.com | | 2 | example2.com | https:/
+----+--------------+--------------------------+
| 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\u Name
列为空。Correct\u URL
正确的URL
栏
我面临的问题是如何在域之前找到前缀部分。它可以是http://
或http://www.
或https://
或https://www.
如何使用PHP正确找到上面4中的内容?请注意,我需要对所有1000000个域运行代码。。。。所以我正在寻找一种最快的检查方法 除了向每种可能性发出HTTP请求并查看是否得到响应之外,没有其他方法了。 当您断言“它可以是http://or.或https://or.”时,现实世界的域可能会提供零、部分或全部或那些(以及各种其他域),并且它们可能会使用ok或重定向或身份验证错误等来响应请求 HTTP和HTTPS不是web应用程序的属性;它们是由端点(web服务器或应用程序防火墙等)处理的通信协议
与任何网络通信一样,必须分别探测主机(“www”在本例中是主机)和端口(不一定,但最常见的是)端口80和443。此探测是一种叫喊,然后您等待并查看是否有服务在另一端侦听。给定已知url,您可以使用
get\u头调用http和/或https版本,从它们的头中可以确定https是否可用,http是否重定向到https等等
可以在此处找到详细信息:您可以使用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
}
因此,我不得不构建一个类似的系统,验证用户提供的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?
}
}
或者基于Alexander Holman的答案,我完全忘记了get\u 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,我已经使用并扩充了这些代码,以更好地满足您的需要。“它可以是http://
或http://www.
或https://
或https://www.
“……或者可能是上述所有…?一个站点可能被设置为响应所有四个“版本”——或者它可能重定向到一个“主”版本。除了发出一个实际的HTTP请求(响应请求,以防站点根本不想对其中一些地址进行应答)之外,没有其他方法可以解决这个问题……而且对于1000000个域来说,这不会很快实现,这一点从一开始就显而易见了。。。。或者干脆禁止您的源IP地址。@YvesLeBorg-不太可能,除非所有域名都由同一实体托管。。。。基本上是的,除了我在一个负载/平衡器和两个产品的安全体系结构中传递了一组域/子域(.net、.io、.ca、.com)。同样的窥探者攻击了所有人(观察到),但没有多少次。@Quentin作为社区维基,你是如何回答的?@iamthemost-stupidperson-我测试过这个。它总是转到else部分(//站点没有响应任何URL,您需要在这里做些什么吗?)。你有什么想法吗?我在代码中安装了bug,$bestUrl
和$best\u url
这很好用。谢谢,但我不明白这是怎么回事。当我们告诉程序添加https://www.
或https://
和域名前面的其他内容(facebook.com.google.com),其中也有一个小错误。当我们经过fb.com时,它会显示真实的URL作为facebook.com。但是它应该显示为没有真正的url。好吧,这就是它的工作原理,想象一下你在浏览器中键入fb.com
,最终得到的url是什么?以上代码就是这么做的。它返回实际的重定向url