Page 1 of 1

file_get_contents() and localhost

Posted: Fri Mar 23, 2007 6:14 am
by frankieta
Hi everyone!

I have a problem in getting a web page through a file_get_contents() request.

If a make

Code: Select all

echo file_get_contents("www.google.com");
everything is fine.

But if I have a script on my pc, that does something and at the end returns a web page doing so:

Code: Select all

echo file_get_contents("http://localhost/myscript.php");
simply displays the source code of my script!

In addition my script accept parameters passed via get, so if a make such a request

Code: Select all

echo file_get_contents("http://localhost/myscript.php?parameter1=par");
it gives me an error: failed to open stream: HTTP request failed!

If I put

Code: Select all

http://localhost/myscript.php?parameter1=par
in my browser it works fine, getting me the response I want.

How can I manage to do the same thing via a php script?

Thanks

Posted: Fri Mar 23, 2007 7:07 am
by aaronhall
I can't speak to the problem you're having, but why are you trying to access the output of a file that's on the same? You might look into using include() or require() as alternatives.

edit: maybe I can speak to those problems -- see if the following works.. if it does, PHP may not be recognizing localhost as a URL (?):

Code: Select all

echo file_get_contents('http://www.somesite.com/myscript.php');
somesite.com maps to 127.0.0.1, and PHP should recognize this as a URL.

Posted: Fri Mar 23, 2007 8:24 am
by frankieta
Thanks for the answer, incredibly it was not functioning at all the connection, also with the browser (maybe a problem at the sites where my script connects to).

Five minutes later it was all right.

I was getting crazy, now I still don't know what happened... :?

Thanks!

Posted: Fri Mar 23, 2007 8:43 am
by feyd
Probably something to do with caching.