-
Jul 9th, 2011, 06:15 AM
#1
Thread Starter
Fanatic Member
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.
-
Jul 9th, 2011, 11:19 AM
#2
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");
-
Jul 10th, 2011, 11:31 AM
#3
Thread Starter
Fanatic Member
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
-
Jul 10th, 2011, 11:47 AM
#4
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.
-
Jul 10th, 2011, 11:50 AM
#5
Thread Starter
Fanatic Member
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.
-
Jul 10th, 2011, 02:44 PM
#6
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.
-
Jul 10th, 2011, 03:01 PM
#7
Thread Starter
Fanatic Member
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.
-
Jul 10th, 2011, 05:25 PM
#8
Re: include an external file
Originally Posted by kows
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.
-
Jul 10th, 2011, 05:41 PM
#9
Thread Starter
Fanatic Member
Re: include an external file
Thats all i want is the output
-
Jul 10th, 2011, 06:59 PM
#10
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.
-
Jul 10th, 2011, 07:17 PM
#11
Thread Starter
Fanatic Member
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
-
Jul 10th, 2011, 10:07 PM
#12
Re: include an external file
Great philosopher once say, "output cannot be derived where output does not exist."
-
Jul 11th, 2011, 11:25 AM
#13
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.
-
Jul 11th, 2011, 11:34 AM
#14
Thread Starter
Fanatic Member
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.
-
Jul 11th, 2011, 11:48 AM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|