anything Posted September 7, 2009 Share Posted September 7, 2009 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! Link to comment https://forums.phpfreaks.com/topic/173441-file_get_contents-with-a-url-returned-from-a-function/ Share on other sites More sharing options...
thebadbad Posted September 7, 2009 Share Posted September 7, 2009 You need to store what's returned by your function in $input: $input = get_user_input(200); Link to comment https://forums.phpfreaks.com/topic/173441-file_get_contents-with-a-url-returned-from-a-function/#findComment-914291 Share on other sites More sharing options...
anything Posted September 7, 2009 Author Share Posted September 7, 2009 Whoops, thanks! Link to comment https://forums.phpfreaks.com/topic/173441-file_get_contents-with-a-url-returned-from-a-function/#findComment-914323 Share on other sites More sharing options...
anything Posted September 7, 2009 Author Share Posted September 7, 2009 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! Link to comment https://forums.phpfreaks.com/topic/173441-file_get_contents-with-a-url-returned-from-a-function/#findComment-914331 Share on other sites More sharing options...
thebadbad Posted September 14, 2009 Share Posted September 14, 2009 It doesn't work that way. Have a read about variable scope here: http://php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/173441-file_get_contents-with-a-url-returned-from-a-function/#findComment-918519 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.