问题描述:

I m using codes like

<?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

?>

to check if a url exists but it is not working for sites which are using ssl conection, i mean https://www.example.com type sites

网友答案:

I don't know if you can use get_headers with https.

But as an alternative (if you have Curl enabled) you can use the following function:

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

If you just need the HTTP status code you can modify the function like this:

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

If you don't have Curl you could try the following function:

<?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 "[email protected]$url_info['path']."?"[email protected]$url_info['query'];
           $head .= " HTTP/1.0\r\nHost: "[email protected]$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;
       }
   }

?>

Note that for HTTPS support you should have SSL support enabled. (uncomment extension=php_openssl.dll in php.ini).

If you can't edit your php.ini and don't have SSL support it will be difficult to get the (encrypted) headers.

You can check your wrappers (openssl and httpd) with:

$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>";

You can check this question on SO for a similar problem.

相关阅读:
Top