SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get_headers() in PHP 5, for PHP 4.4.4 ?

    Please help, after I develope the code in my localhost which is using PHP 5 and i use function get_headers(), without my realization that my web hosting is using php 4.4.4 only and it does not support get_headers, so is there a function which is equivalent to get_headers() in php 4.4.4 ? Thank you...

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php manual
    This is a modified version of code from "stuart at sixletterwords dot com", at 14-Sep-2005 04:52. This version tries to emulate get_headers() function at PHP4. I think it works fairly well, and is simple. It is not the best emulation available, but it works.
    Features:
    - supports (and requires) full URLs.
    - supports changing of default port in URL.
    - stops downloading from socket as soon as end-of-headers is detected.
    Limitations:
    - only gets the root URL (see line with "GET / HTTP/1.1").
    - don't support HTTPS (nor the default HTTPS port).
    PHP Code:
    <?php
    if(!function_exists('get_headers'))
    {
    function  
    get_headers($url,$format=0)
    {
    $url=parse_url($url);
    $end =  "\r\n\r\n";
    $fp fsockopen($url['host'],  (empty($url['port'])?80:$url['port']), $errno$errstr30);
    if  (
    $fp)
    {
    $out  "GET / HTTP/1.1\r\n";
    $out .= "Host:  ".$url['host']."\r\n";
    $out .= "Connection: Close\r\n\r\n";
    $var  =  '';
    fwrite($fp$out);
    while (!
    feof($fp))
    {
    $var.=fgets($fp,  1280);
    if(
    strpos($var,$end))
    break;
    }
    fclose($fp);
    $var=preg_replace("/\r\n\r\n.*\$/",'',$var);
    $var=explode("\r\n",$var);
    if(
    $format)
    {
    foreach(
    $var  as $i)
    {
    if(
    preg_match('/^([a-zA-Z -]+):  +(.*)$/',$i,$parts))
    $v[$parts[1]]=$parts[2];
    }
    return  
    $v;
    }
    else
    return 
    $var;
    }
    }
    }
    ?>
    check the manual for better options.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh my goodness, all these long code is equivalent to one phrase of code which is get_hearders();
    I think i am going to ask my web hosting to migrate me to PHP 5, though they charge with a little fees. Thank's Kailash Badu for your help of now and before also.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •