如何检查php中是否存在https站点
我使用的代码如下如何检查php中是否存在https站点,php,url,https,Php,Url,Https,我使用的代码如下 <?php $url = 'http://www.example.com'; if(isset($_GET['url'])){$url = $_GET['url'];} $array = get_headers($url); $string = $array[0]; if(strpos($string,"200")){ echo 'url exists'; } else{ echo 'url does not exist'; } //
<?php
$url = 'http://www.example.com';
if(isset($_GET['url'])){$url = $_GET['url'];}
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")){
echo 'url exists';
}
else{
echo 'url does not exist';
}
//this code does not works for ssl connection
?>
要检查url是否存在,但它对于使用ssl连接的站点不起作用,我的意思是键入sites我不知道您是否可以将
get_headers
与https一起使用
但作为替代方案(如果启用了Curl),您可以使用以下函数:
function getheaders($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_NOBODY, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($c, CURLOPT_URL, $url);
$headers = curl_exec($c);
curl_close($c);
return $headers;
}
<?php
function my_get_headers($url ) {
$url_info=parse_url($url);
if (isset($url_info['scheme']) && $url_info['scheme'] == 'https') {
$port = 443;
@$fp=fsockopen('ssl://'.$url_info['host'], $port, $errno, $errstr, 10);
} else {
$port = isset($url_info['port']) ? $url_info['port'] : 80;
@$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10);
}
if($fp) {
stream_set_timeout($fp, 10);
$head = "HEAD ".@$url_info['path']."?".@$url_info['query'];
$head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
fputs($fp, $head);
while(!feof($fp)) {
if($header=trim(fgets($fp, 1024))) {
$sc_pos = strpos( $header, ':' );
if( $sc_pos === false ) {
$headers['status'] = $header;
} else {
$label = substr( $header, 0, $sc_pos );
$value = substr( $header, $sc_pos+1 );
$headers[strtolower($label)] = trim($value);
}
}
}
return $headers;
}
else {
return false;
}
}
?>
如果您只需要HTTP状态代码,可以修改如下函数:
function getstatus($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_NOBODY, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($c, CURLOPT_URL, $url);
curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
return $status;
}
如果没有Curl,可以尝试以下函数:
function getheaders($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLOPT_NOBODY, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($c, CURLOPT_URL, $url);
$headers = curl_exec($c);
curl_close($c);
return $headers;
}
<?php
function my_get_headers($url ) {
$url_info=parse_url($url);
if (isset($url_info['scheme']) && $url_info['scheme'] == 'https') {
$port = 443;
@$fp=fsockopen('ssl://'.$url_info['host'], $port, $errno, $errstr, 10);
} else {
$port = isset($url_info['port']) ? $url_info['port'] : 80;
@$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10);
}
if($fp) {
stream_set_timeout($fp, 10);
$head = "HEAD ".@$url_info['path']."?".@$url_info['query'];
$head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n";
fputs($fp, $head);
while(!feof($fp)) {
if($header=trim(fgets($fp, 1024))) {
$sc_pos = strpos( $header, ':' );
if( $sc_pos === false ) {
$headers['status'] = $header;
} else {
$label = substr( $header, 0, $sc_pos );
$value = substr( $header, $sc_pos+1 );
$headers[strtolower($label)] = trim($value);
}
}
}
return $headers;
}
else {
return false;
}
}
?>
请注意,对于HTTPS支持,您应该启用SSL支持。(php.ini中的uncomment extension=php_openssl.dll)
如果您不能编辑php.ini,并且没有SSL支持,那么将很难获取(加密的)标题
您可以通过以下方式检查包装器(openssl和httpd):
$w=stream\u get\u wrappers();
回显“openssl:”,已加载扩展名(“openssl”)?'“是”:“否”,“
\n”;
回送“http包装器:”,在_数组中('http',$w)?'“是”:“否”,“
\n”;
回送“https包装器:”,在_数组中('https',$w)?'“是”:“否”,“
\n”;
echo“wrappers:”,var_dump($w),“
”;
您可以检查是否存在类似问题。之后$array的内容是什么?该数组包含从该url接收到的完整标题信息不存在。我已使用此域进行了检查。代码工作正常。问题是当我尝试url时,如未启用CURL…:(如果未启用curl,则在我的答案中添加了一些信息。新函数。(但需要启用ssl模块才能获取https标头)。如果启用
php_openssl.dll
,可能get_headers
会再次用于https站点。谢谢!这对我来说很有效。我在检查https站点存在时遇到了问题,因为它总是检测到站点不存在,即使它确实存在。添加了curl_setopt($c,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($c,CURLOPT_SSL_VERIFYHOST,true);成功了!:)