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

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