douhuangjian9627 2013-07-24 11:06
浏览 48
已采纳

too long

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

  • 写回答

1条回答 默认 最新

  • dongmi3203 2013-07-24 11:37
    关注

    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
    Host: ".@$url_info['host']."
    
    ";
               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>
    ";
    echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "<br>
    ";
    echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "<br>
    ";
    echo 'wrappers: <pre>', var_dump($w), "<br>";
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?