Gaias
Gaias

Reputation: 45

Get local PHP page as external client

I wish to call a local PHP page with PHP with the server side code already executed.

For instance, if I have a page with the following content:

Hello, this is <?php $name="John"; echo $name ?>

I wish to have a get command such as file_get_contents(local_php_page) or fopen(handle) return:

Hello, this is John

Rather than

Hello, this is <?php $name="John"; echo $name ?>

What's the best way to do this?

Upvotes: 2

Views: 174

Answers (4)

sumit
sumit

Reputation: 15464

Need to call http url instead of path

$url="http://".$_SERVER['HTTP_HOST']."/yourpath/";
$str=file_get_contents($url."/yourfile.php");
echo $str;

Upvotes: 0

yunzen
yunzen

Reputation: 33439

If you have allow_url_fopen in your server, you might use fopen with an url.

Upvotes: 0

Daniel Freudenberger
Daniel Freudenberger

Reputation: 352

As you've already mentioned you can use file_get_contents to send a get request to any http url. You have to make sure, that allow_url_fopen is enabled. Of course your webserver has to be configured to handle php requests correctly

<?php
$requestUrl = 'http://localhost/your_file.php';
$content = file_get_contents($requestUrl);
var_dump($content);

Upvotes: 0

Jake M
Jake M

Reputation: 544

Output Buffering should be able to do that for you:

ob_start();
include 'myfile.php';
$xhtml = ob_get_clean();

You could also get the output of your include, eg:

$xhtml = include 'myfile.php';

For more on this, check out The PHP Manual

Upvotes: 1

Related Questions