Results 1 to 15 of 15

Thread: include an external file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    include an external file

    Hi,

    I want to include an external php file inside a php file, so if i use a php file on: www.test.com i want to include a file from www.site.com

    Ive looked a several methods and cant come up with a solution.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: include an external file

    If you want to execute the actual PHP file from test.com on site.com, this is something you just can't do. Once PHP has processed the file and sent it to the webserver, it's just plain HTML.

    If you only want to include the output of the file on test.com on your website, you could use something like file_get_contents():
    PHP Code:
    echo file_get_contents("http://test.com/file.php"); 
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: include an external file

    That doesnt seem to work. I got this error (the url is only an example url for the purpose of this post):

    Code:
    Warning [2] file_get_contents(http://www.test.com/test.php) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: include an external file

    If you get a 404 error, it means whatever page you're trying to access doesn't exist. Which means you need to provide file_get_contents a URL that does exist.

    As stated by others in this previous post of yours, the code you're using works fine. If you want to get more help, you're probably going to need to actually post the URL you're trying to use (NOT an example URL), so that we can confirm that it either is or isn't working for us -- and perhaps why.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: include an external file

    I can assure you that the page exists.

    Ok, Im using this now:

    Code:
    $f="http://www.test.com/test.php";
    $data = file_get_contents($f);
    echo $data;
    Im not getting any errors with it, but its not outputing the contents of /test.php
    Last edited by dunlop03; Jul 10th, 2011 at 12:33 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: include an external file

    This code works perfectly fine:
    PHP Code:
    <?php
      
    echo file_get_contents("http://cowarama.com");
    ?>
    If your code doesn't work, then it has to do with the URL and domain that you're trying to request data from. Without knowing that URL, no one can help you. Seriously.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: include an external file

    But what content are you getting from "http://cowarama.com" I want to get content from a .php file "/test.php"

    Basically I dont want a file to run on a users server unless it has a specific file on my server.
    Last edited by dunlop03; Jul 10th, 2011 at 04:02 PM.

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: include an external file

    Quote Originally Posted by kows View Post
    If you want to execute the actual PHP file from test.com on site.com, this is something you just can't do. Once PHP has processed the file and sent it to the webserver, it's just plain HTML.

    If you only want to include the output of the file on test.com on your website, you could use something like file_get_contents():
    PHP Code:
    echo file_get_contents("http://test.com/file.php"); 
    You cannot retrieve the script file from another server. You can only return its output.
    Like Archer? Check out some Sterling Archer quotes.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: include an external file

    Thats all i want is the output

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: include an external file

    .. Then file_get_contents() gets exactly what you want. I'm not sure why this is such a difficult thing to understand: no one can help you unless you post the URL you're trying to open. Otherwise, you're just wasting time here -- someone is going to have to look at and figure out why the URL you're trying to open does not work, and we need the exact URL you're trying to open in order to check that.

    If you're not willing to do that, then the one line of code I've posted above does exactly what you want it to do.
    Like Archer? Check out some Sterling Archer quotes.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: include an external file

    Ok heres the actual domain

    Code:
    http://mafiahunt.com/test.php
    See if you can get the output of that

  12. #12
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: include an external file

    Great philosopher once say, "output cannot be derived where output does not exist."

  13. #13
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: include an external file

    The actual domain is not what's important; the script (or full, exact, complete URL) you're trying to get data from is. The script you're linking to outputs nothing. Open it in your browser, then view the source.
    Like Archer? Check out some Sterling Archer quotes.

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: include an external file

    Ok maybe ive not been clear lol. The "http://mafiahunt.com/test.php" file only contains php, it only has 1 small function in it. So when a user runs the other file i want that file to include the function from "http://mafiahunt.com/test.php" , if "http://mafiahunt.com/test.php" is not there the users file wont run.

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: include an external file

    first you need to understand the difference between the output and the contents of the file... and then you can see (hopefully) why you're having issues... the CONTENT of the file is your actual raw PHP code... the OUTPUT of the file is the rendered HTML that is sent to the browser... one doesn't necessarily correlate to the other, other than the PHP code can output HTML which will be sent to the browser.

    you can only include and run it IF you are running on the same domain... you CANNOT include a file from DomainA and "run" it on DomainB ... at best you can only get its rendered output using the methods outlined above... BUT it has to produce an output. In this case since all it has is a single function, there is no output... so the get contents fails - there were no contents to get.

    Despite what the name implies, get_contents() doesn't actually get the contents... what it does is a http get on the file, causing it to run (if it's PHP or ASP) and returns the results. If the file is a text file or a non-server script file, then you may actually get the file itself... but it depends on how the server interprets the request and sends the data back.

    Now, if you want to include a PHP file that has a function you want to call, then you use the include(), require(), include_one() or require_once() functions.... BUT IT HAS GOT to be on the same domain... to be able to include files from alternate domains would be a security risk... there's a reason it can't (or at least shouldn't) be done. That said, if you own both domains, and both are on the same server, depending on your setup, you may be able to share the file locally...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width