PHP_Compat
[ class tree: PHP_Compat ] [ index: PHP_Compat ] [ all elements ]

Source for file get_headers.php

Documentation is available at get_headers.php

  1. <?php
  2. define('PHP_COMPAT_GET_HEADERS_MAX_REDIRECTS', 5);
  3.  
  4. /**
  5.  * Replace get_headers()
  6.  *
  7.  * @category    PHP
  8.  * @package     PHP_Compat
  9.  * @license     LGPL - http://www.gnu.org/licenses/lgpl.html
  10.  * @copyright   2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
  11.  * @link        http://php.net/function.get_headers
  12.  * @author      Aeontech <aeontech@gmail.com>
  13.  * @author      Cpurruc <cpurruc@fh-landshut.de>
  14.  * @author      Aidan Lister <aidan@php.net>
  15.  * @author      Arpad Ray <arpad@php.net>
  16.  * @version     $Revision: 269597 $
  17.  * @since       PHP 5.0.0
  18.  * @require     PHP 4.0.0 (user_error)
  19.  */
  20. function php_compat_get_headers($url, $format = 0)
  21. {
  22.     $result = array();
  23.     for ($i = 0; $i < PHP_COMPAT_GET_HEADERS_MAX_REDIRECTS; $i++) {
  24.         $headers = php_compat_get_headers_helper($url, $format);
  25.         if ($headers === false) {
  26.             return false;
  27.         }
  28.         $result = array_merge($result, $headers);
  29.         if ($format == 1 && isset($headers['Location'])) {
  30.             $url = $headers['Location'];
  31.             continue;
  32.         }
  33.         if ($format == 0) {
  34.             for ($j = count($headers); $j--;) {
  35.                 if (preg_match('/^Location: (.*)$/i', $headers[$j], $matches)) {
  36.                     $url = $matches[1];
  37.                     continue 2;
  38.                 }
  39.             }
  40.         }
  41.         return $result;
  42.     }
  43.     return empty($result) ? false : $result;
  44. }
  45.  
  46. function php_compat_get_headers_helper($url, $format)
  47. {
  48.     // Init
  49.     $urlinfo = parse_url($url);
  50.     $port    = isset($urlinfo['port']) ? $urlinfo['port'] : 80;
  51.  
  52.     // Connect
  53.     $fp = fsockopen($urlinfo['host'], $port, $errno, $errstr, 30);
  54.     if ($fp === false) {
  55.         return false;
  56.     }
  57.           
  58.     // Send request
  59.     $head = 'HEAD ' . (isset($urlinfo['path']) ? $urlinfo['path'] : '/') .
  60.         (isset($urlinfo['query']) ? '?' . $urlinfo['query'] : '') .
  61.         ' HTTP/1.0' . "\r\n" .
  62.         'Host: ' . $urlinfo['host'] . "\r\n\r\n";
  63.     fputs($fp, $head);
  64.  
  65.     // Read
  66.     $headers = array();
  67.     while (!feof($fp)) {
  68.         if ($header = trim(fgets($fp, 1024))) {
  69.             list($key) = explode(':', $header);
  70.  
  71.             if ($format === 1) {
  72.                 // First element is the HTTP header type, such as HTTP 200 OK
  73.                 // It doesn't have a separate name, so check for it
  74.                 if ($key == $header) {
  75.                     $headers[] = $header;
  76.                 } else {
  77.                     $headers[$key] = substr($header, strlen($key)+2);
  78.                 }
  79.             } else {
  80.                 $headers[] = $header;
  81.             }
  82.         }
  83.     }
  84.  
  85.     fclose($fp);
  86.  
  87.     return $headers;
  88. }
  89.  
  90. // Define
  91. if (!function_exists('get_headers')) {
  92.     function get_headers($url, $format = 0)
  93.     {
  94.         return php_compat_get_headers($url, $format);
  95.     }
  96. }

Documentation generated on Mon, 11 Mar 2019 15:39:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.