Jump to content

file_get_contents with a URL returned from a function


anything

Recommended Posts

Hi, I'm having a file_get_contents problem.  I'll try to learn cURL later.

If I enter a URL into a variable and then try to read that URL with file_get_contents directly it works, such as: 

 

            echo "Enter the URL you want to get:\n";

            $fr = fopen("php://stdin", "r");

            $input = fgets($fr, 200);

            $input = rtrim($input);

            fclose($fr);

    $data = file_get_contents($input);

 

With this code, say the user entered http://www.google.com, that URL is entered into the $input variable, and file_get_contents($input) will correctly get the contents from http://www.google.com and put the contents into the $data variable.

 

However, if I make this into a function, such as:

 

$input=' ';

$data=' ';

function get_user_input($length)

{

            echo "Enter the URL you want to get:\n";

            $fr = fopen("php://stdin", "r");

            $input = fgets($fr, $length);

            $input = rtrim($input);

            fclose($fr);

    return $input;

}         

get_user_input(200);

echo $input;

$data = file_get_contents($input);

if ($data)

{echo "There is data downloaded";}

else

{echo "There is no data downloaded";}

 

When I make this into a function as above, when a user enters something like http://www.google.com, and that is returned as $input from the function, it can correctly echo $input as http://www.google.com, but file_get_contents($input) does not correctly get the contents from http://www.google.com, and the script echoes ""There is no data downloaded."  I've tried to use things like

 

$input = urlencode($input);

$input = strval($input);

$input = trim($input);

 

to see if returning $input from the function added unnecessary characters to $input that is interfering with file_get_contents($input), but none of it works.  I've tried putting $data = file_get_contents($input) into the function and returning $data instead of having that code after the function, but that doesn't work either. 

 

Can you please explain to me why the first script works and the second one doesn't?  I'm not sure what would make the difference.

 

Thanks!

 

 

Also, what is the terminology for this? How would you describe the state of  $input when it was returned from the function using the code "return $input"--even though I didn't assign $input=get_user_input(200) yet I could echo $input correctly.  However, I couldn't yet use $input in file_get_contents until I assigned the result of the function with the code "$input=get_user_input(200)" as you suggested.  What is the terminology for this difference?  What is the name for this phenomenon, why could I echo $input but not use it in file_get_contents until I did the assignment $input=get_user_input(200)?  Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.