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.
Printable View
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.
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");
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
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.
I can assure you that the page exists.
Ok, Im using this now:
Im not getting any errors with it, but its not outputing the contents of /test.phpCode:$f="http://www.test.com/test.php";
$data = file_get_contents($f);
echo $data;
This code works perfectly fine:
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.PHP Code:<?php
echo file_get_contents("http://cowarama.com");
?>
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.
Thats all i want is the output :(
.. 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.
Ok heres the actual domain
See if you can get the output of that ;)Code:http://mafiahunt.com/test.php
Great philosopher once say, "output cannot be derived where output does not exist."
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.
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.
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