file_get_contents() and localhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frankieta
Forum Newbie
Posts: 2
Joined: Fri Mar 23, 2007 6:02 am

file_get_contents() and localhost

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
frankieta
Forum Newbie
Posts: 2
Joined: Fri Mar 23, 2007 6:02 am

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Probably something to do with caching.
Post Reply