Code To List HTTP headers with PHP

Discussion in 'PHP' started by SNaRe, Dec 21, 2006.

  1. #1
    I need a code to list http headers with php . Can someone help ?
     
    SNaRe, Dec 21, 2006 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    perhaps you could give us an example of what you meant by that ?
     
    krakjoe, Dec 21, 2006 IP
  3. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #3
    HTTP Header
    http://web-sniffer.net/
    you can grab http headers of websites from here. And i want to grab this myself.
     
    SNaRe, Dec 21, 2006 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?php
    $url = 'http://www.youtube.com/watch?v=B8H29jU8Wrs';
    echo "<pre>";
    print_r(get_headers($url, 1));
    ?> 
    
    PHP:
     
    krakjoe, Dec 21, 2006 IP
  5. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #5
    I want to grab
    HTTP/1.1 303 headers instead of HTTP/1.1 200
    How will i do
     
    SNaRe, Dec 21, 2006 IP
  6. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #6
    function get_headers_x($url,$format=0, $user='', $pass='', $referer='') {
           if (!empty($user)) {
               $authentification = base64_encode($user.':'.$pass);
               $authline = "Authorization: Basic $authentification\r\n";
           }
    
           if (!empty($referer)) {
               $refererline = "Referer: $referer\r\n";
           }
    
           $url_info=parse_url($url);
           $port = isset($url_info['port']) ? $url_info['port'] : 80;
           $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30);
           if($fp) {
               $head = "GET ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\n";
               if (!empty($url_info['port'])) {
                   $head .= "Host: ".@$url_info['host'].":".$url_info['port']."\r\n";
               } else {
                   $head .= "Host: ".@$url_info['host']."\r\n";
               }
               $head .= "Connection: Close\r\n";
               $head .= "Accept: */*\r\n";
               $head .= $refererline;
               $head .= $authline;
               $head .= "\r\n";
    
               fputs($fp, $head);     
               while(!feof($fp) or ($eoheader==true)) {
                   if($header=fgets($fp, 1024)) {
                       if ($header == "\r\n") {
                           $eoheader = true;
                           break;
                       } else {
                           $header = trim($header);
                       }
    
                       if($format == 1) {
                       $key = array_shift(explode(':',$header));
                           if($key == $header) {
                               $headers[] = $header;
                           } else {
                               $headers[$key]=substr($header,strlen($key)+2);
                           }
                       unset($key);
                       } else {
                           $headers[] = $header;
                       }
                   }
               }
               return $headers;
    
           } else {
               return false;
           }
       }
    
    $response = get_headers_x($url, 1);
    
    PHP:
     
    mad4, Dec 21, 2006 IP
    vishwaa likes this.
  7. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Itried this code but it's also giving 200 header status i need 303 header status
     
    SNaRe, Dec 21, 2006 IP
  8. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #8
    mad4, Dec 21, 2006 IP
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    I wrote one too, and still get 200 headers, but know it gets everything, little confused....
     
    krakjoe, Dec 21, 2006 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    And here I was thinking that with a single request, you normally only got one status header back. If you want a 303, don't you have to basically trick the server in to returning one? I mean, you can't just point at a random server and expect a particular status header to come back, right?
     
    TwistMyArm, Dec 21, 2006 IP
  11. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Exactly. The script outputs what the server sends back.
     
    mad4, Dec 21, 2006 IP