Problem getting file contents

Discussion in 'PHP' started by jmrozi1, Aug 31, 2009.

  1. #1
    I'm trying to get the contents of a site hosted on a local server. Normally, I'd just do this:
    file_get_contents("https://www.sampleurl.com")
    Code (markup):
    Unfortuantely, the function is unable to open the file for security purposes. Since the file is local, my next choice was to try this method:
    file_get_contents("path/to/file.php")
    Code (markup):
    This time, the function doesn't work because I'm trying to get what the file would display rather than what the file actually contains. Imagine, for example, that file.php was this:
    
    require("header.html");
    require("content.html");
    require("footer.html");
    
    Code (markup):
    I would only get the require statements using the file_get_contents function.

    Finally, I tried this method:
    
    echo "<div style='display:none'>";
    require("path/to/file.php");
    echo "</div>";
    
    Code (markup):
    But this doesn't return the text; rather, it executes the text and prints the results. The only way I can think of to recover this data would be using JavaScript.

    It seems like there should be a compile a php file and store the text output in a variable, but for the life of me I can't figure it out. I'm open to any suggestions.

    Thanks!
     
    jmrozi1, Aug 31, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Actually, for the third method you can use ob_start:

    ob_start();
    require 'path/to/file.php';
    $output = ob_get_contents();
    PHP:
    As far as the first method, you may want to check if your host supports curl..

    // create a new cURL resource
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    PHP:
     
    premiumscripts, Aug 31, 2009 IP
  3. jmrozi1

    jmrozi1 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The host doesn't support curl, but the ob_start() / ob_get_clear() method worked perfectly!

    I can't even express how much time and stress you just saved me. Thanks for your help!
     
    jmrozi1, Aug 31, 2009 IP