...

View Full Version : file_get_contents() and localhost



frankieta
03-23-2007, 01:17 PM
Hi everyone!

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

If a make


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:


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


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:
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

rafiki
03-23-2007, 04:26 PM
include('file.php?param1=para');
wat bout that?

iLLin
03-23-2007, 05:54 PM
As far as I know you cant pass variables when using require/require_once/include or file_get_contents. It searches for that specific file. myscript.php?parameter1=par Your file name would have to be that...

If you want to pull (import/include w/e) a script with variables attached, you need to add parameters to it.

If your using php5 you can use file_get_contents like so:



$params = array('http' => array(
'method' => 'POST',
'content' => $data
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
$response = @stream_get_contents($fp);


You can view a more detailed function at Wez's blog here: http://netevil.org/node.php?nid=937 .

If you don't have php5 then you would have to use sockets to open a file with parameters attached or use AJAX or cURL. I recommend AJAX :).

Hope this helps some.

Inigoesdr
03-23-2007, 06:19 PM
As far as I know you cant pass variables when using require/require_once/include or file_get_contents. It searches for that specific file. myscript.php?parameter1=par Your file name would have to be that...
file_get_contents() works with GET variables because it's part of the requested URL, and include will work too if it's allowed by your host(include*/require* use fopen wrappers for urls when enabled). In order for either to work allow_url_fopen and allow_url_include for PHP 5.2+ have to be enabled in the php.ini.

iLLin
03-23-2007, 06:30 PM
file_get_contents() works with GET variables because it's part of the requested URL, and include will work too if it's allowed by your host(include*/require* use fopen wrappers for urls when enabled). In order for either to work allow_url_fopen and allow_url_include for PHP 5.2+ have to be enabled in the php.ini.

See another reason I should upgrade to php5 :p

CFMaBiSmAd
03-23-2007, 07:36 PM
allow_url_fopen has existed since early PHP4 versions.

allow_url_include was added in PHP 5.2 to allow you to restrict the allow_url_fopen setting for include/require functions. Prior to this, allow_url_fopen alone controlled if you could use a file wrapper in include/require functions.

firepages
03-24-2007, 12:52 AM
to the original poster, your code should work, there is nothing wrong with your code or logic, are you on windows or linux or ??
If http://localhost works in the browser then the code should work, but try adding a line to /etc/hosts (or c:/windows/system32/drivers/etc/hosts)

127.0.0.1 test.domain.com

then try your code with test.domain.com instead of localhost



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum