2

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

Minhaz
  • 937
  • 1
  • 10
  • 25

1 Answers1

5

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 ".@$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;
       }
   }

?>

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.

Community
  • 1
  • 1
Rik
  • 1,982
  • 1
  • 17
  • 30
  • CURL is not enabled... :( – Minhaz Jul 24 '13 at 11:42
  • 1
    Added some info to my answer if curl is not enabled. New function. (but you need ssl-module enabled to get https headers). Maybe `get_headers` works again with https sites if you enable `php_openssl.dll`. – Rik Jul 24 '13 at 13:43
  • 1
    Thanks! This worked for me. I was having trouble checking https site existence as it always detects site not exist even if it does exist. added curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($c, CURLOPT_SSL_VERIFYHOST, true); and it worked! :) – Woppi Oct 07 '13 at 03:13