file_get_contents not working

Discussion in 'Coding Help' started by amber.long83, Nov 12, 2009.

Tags:
  1. amber.long83

    amber.long83 New Member

    Hello all,

    This is my code:

    PHP Code:
    $url = "Show.php?ShowID=1";
    $str = file_get_contents($url);
    echo $str;

    That's supposed to get the contents of show.php with showID as 1. But this doesn't work. Just typing show.php works, but the ?showID=1 does not work.

    How could I make it so that it works?

    Thanks
     
  2. php.allstar

    php.allstar New Member

    Hi,

    You are calling the file in the local filesystem. This method does not support parameters.

    If you use a http wrapper for your function call you will be able to pass parameters

    Be careful with this function and ensure that you don't have allow_url_fopen enabled in your php.ini file, although this is generally disabled on webhosts who know what they are doing.

    Personally, I'd use cURL to include file contents to a variable although you may have to enable cURL on your server for this...

    <?php

    function includeFile($url) {

    // Initiate the curl session
    $ch = curl_init();
    $timeout = 5;

    // Point curl to the irl we want
    curl_setopt($ch, CURLOPT_URL, $url);

    // Set some cURL options
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    // Get the file contents
    $file_contents = curl_exec($ch);

    // Close the curl session
    curl_close($ch);

    $content = implode('', array($file_contents));

    // Return the content
    return $content;

    }

    $str = includeFile("http://www.domain.tld/Show.php?ShowID=1");

    ?>

    You could also try something like this to just initialise your variable before your call file_get_contents, then tell show.php to do something if the $showID variable isset...

    <?php

    $showID = 1;

    $url = "Show.php";
    $str = file_get_contents($url);
    echo $str;

    ?>
     
  3. amber.long83

    amber.long83 New Member

    thanks for give me solution
     

Share This Page