Michel
Michel

Reputation: 11749

How do I include an external file in PHP?

I need to include a file (belonging to another server) inside a php web page.

More precisely : let us assume I have this web page : http://www.example.com/mypage.html and when viewed in a browser it displays : Hello World !

I want to include this contents (not the source): Hello World !

inside my php page (assume on www.myexampleserver.com). What should I do ?

I tried : include "http://www.example.com/mypage.html";

but it does not work.

I would also like to be able to do something like this. On my php page source :

echo "My other page contains ".SomeCodeThatIDontKnow." and that is all";

and display when viewed in a browser : My other page contains Hello World ! and that is all

Thanks for any indication.

Upvotes: 2

Views: 1265

Answers (4)

Debbie S
Debbie S

Reputation: 1

I've done this on my sites. I manage 3 internal websites which are related so I often share code (and complete source files) between them.

If you don't want to include the source of the other file, you could make the content portion of that file a separate file and then include that in both files.

EG:
www.example.com/ContentSource.php:
hello world and other content.

www.example.com/mypage.php:
source code
include "www.example.com/Contentsource.php";
remainder of source

www.myexampleserver.com/otherpage.php:
source code
My other page contains
include "www.example.com/Contentsource.php";
and that is all.
remainder of source

Then you just manage the content you want displayed in both spots in the one file (ContentSource.php) and include it where you want.

One bank of servers I work on uses MySQL and the other Oracle. On my MySQL servers, I'm including a file located on the Oracle server that displays a table of codes applicable to both websites, but is only available in the Oracle database tables. The Oracle server has the php that creates the html and I just include that in my MySQL server web page - voila - information from another server that I did not have to manually re-create.

If both sites are managed by same person/company, I see no reason why include cannot be used.

Upvotes: 0

lszrh
lszrh

Reputation: 1582

You can also use file_get_contents() or file() for this.

But don't use include/require!!!

If you want to do something you should load the webpage and start actions through the content. Here an example:

The remote webpage returns the content "do-something". So you can check whether the remote file returns this or another phrase and start an function:

$command = file_get_contents('http://remote-domain.tld/file.html');
if($command == 'do-login') {
    //... do something
}
...

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

$otherpage = file_get_contents("http://www.example.com/mypage.html");
echo "My other page contains: " . $otherpage;

If you expect the "other page" to only contain text, you might also want to use htmlentities to ensure there's no HTML injected into your page..

$otherpage = file_get_contents("http://www.example.com/mypage.html");
echo "My other page contains: " . htmlentities($otherpage);

Upvotes: 3

Adrian Serafin
Adrian Serafin

Reputation: 7715

If you want to display this text in your browser look at iframe tag.

Upvotes: 0

Related Questions