如何检查php中是否存在https站点


how to check if a https site exists in php

我正在使用之类的代码

<?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连接的网站,我的意思是https://www.example.com类型站点

我不知道你是否可以将get_headers与https一起使用。

但作为一种替代方案(如果您启用了卷曲),您可以使用以下功能:

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;
}

如果你只需要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;
}

如果你没有卷曲,你可以尝试以下功能:

<?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中取消注释扩展名=php_opensl.dll).

如果您不能编辑php.ini并且没有SSL支持,那么将很难获得(加密的)标头。

您可以使用检查您的包装器(openssl和httpd)

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "<br>'n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "<br>'n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "<br>'n";
echo 'wrappers: <pre>', var_dump($w), "<br>";

您可以在SO上查看此问题以了解类似的问题。