cURL - how to only read a website header?!

Discussion in 'PHP' started by redhits, Sep 12, 2007.

  1. #1
    $url='http://softgroups.com';
    $agent="SoftGroups Header Reader (http://softgroups.com)";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $buffer=curl_exec($ch);
    curl_close($ch);
    echo $buffer;
    ?>

    I want onlt to get the header from the server
    what;'s wrong!? this scrit return me the whole page, lol
     
    redhits, Sep 12, 2007 IP
  2. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #2
    try this:

    
    <?php
    
    $url='http://softgroups.com';
    $agent="SoftGroups Header Reader (http://softgroups.com)";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $buffer = curl_exec($ch);
    $curl_info = curl_getinfo($ch);
    curl_close($ch);
    $header_size = $curl_info[header_size];
    $header = substr($buffer, 0, $header_size);
    
    echo $header;
    ?>
    
    PHP:
    $curl_info = curl_getinfo($ch); to capture the curl request info
    $curl_info[header_size] will contain the curl request header size
    $header = substr($buffer, 0, $header_size); to only get the header by the header size
    finally $header will contain only the header information.
     
    grandpa, Sep 12, 2007 IP
  3. logikcoder

    logikcoder Peon

    Messages:
    154
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You may need to set the NOBODY flag to get only the headers.

    
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    
    PHP:
     
    logikcoder, Sep 12, 2007 IP