Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question file_get_contents() and localhost

    Hi everyone!

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

    If a make

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

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

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

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    include('file.php?param1=para');
    wat bout that?

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
         $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.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by iLLin View Post
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    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

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,037
    Thanks
    2
    Thanked 316 Times in 308 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •