about file_get_contents

Discussion in 'PHP' started by hasanbasri, Jan 24, 2010.

  1. #1
    note the following code
    $ops=array()
     $context = stream_context_create($opts);
        $site="http://www.example.com/aa.php";
        set_time_limit(20);
        $result = @file_get_contents($site, false, $context);
    Code (markup):
    the above code will get the full contents of this page in $result variable >>
    but I want to get directly only header content of this page in order to peform more speed and less ammount transfere and because I don't need the full contents ,I just want contents in the <header>............</header> .

    I don't want to do any extract on the $result variable.

    are there any ideas ??
     
    hasanbasri, Jan 24, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use fopen and fread or fgets and when you find the </head> stop reading and close the connection
     
    JAY6390, Jan 24, 2010 IP
  3. hasanbasri

    hasanbasri Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you for your replay.
    but this page I want to request has a $_POST and $_GET variables and I want to pass these variables to this page.
    I can't pass these variables to this page and request the results using fopen,fread and fgets functions.


    thank you

    any other ideas
     
    hasanbasri, Jan 24, 2010 IP
  4. hasanbasri

    hasanbasri Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hasanbasri, Jan 24, 2010 IP
  5. Bananasphere

    Bananasphere Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Bananasphere, Jan 24, 2010 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That gets the HTTP headers, not the <head> section of an HTML document.
     
    SmallPotatoes, Jan 24, 2010 IP
  7. Bananasphere

    Bananasphere Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yeah, I was just saying that if he was only looking for more specific information which may be available through the HTTP headers it's worth taking a look at.

    - BS :)
     
    Bananasphere, Jan 24, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    This should work:

    <?php
    
    $site="http://www.example.com/aa.php";
    
    $file = file_get_contents($site);
    
    if(preg_match("/<head.*>(.*)<\/head>/smU", $file, $match)){
    
    echo $match[1];
    
    } else {
    
    echo "No match found";
    
    }
    
    ?>
    PHP:
     
    danx10, Jan 24, 2010 IP