Utku Dalmaz
Utku Dalmaz

Reputation: 10172

how to get returned text from a php page

very simple php question,

for example, demo.php just returns a text like this "hello".

how can i get this text from an another php page ?

UPDATE:

actually i meant page is outputting it like this "print 'hello';"

Upvotes: 8

Views: 6292

Answers (5)

karim79
karim79

Reputation: 342665

EDIT: I had initially assumed that executing file_get_contents on a script would grab the output (and not the code). If you want the output, you need to specify the full URL:

$str = file_get_contents("http://example.com/demo.php");

http://php.net/manual/en/function.file-get-contents.php

It would probably be better if you accepted one of the more detailed answers.

Also, see the below:

Upvotes: 2

Andrew Moore
Andrew Moore

Reputation: 95424

Does it return "hello", does it output hello or does it simply contains hello? All three scenarios are different problems.


If it return "hello"; as such:

<?php
return "hello";

then you can easily grab its value by including the file and grabbing the return value:

<?php
$fileValue = include('secondFile.php');

If it outputs hello as such:

<?php
echo "hello"; // or print "hello";

you must use output buffering to capture the result:

<?php
ob_start();
include('secondFile.php');
$fileValue = ob_get_contents();
ob_end_clean();

If it contains hello as such:

hello

you can simply read the result:

<?php
$fileValue = file_get_contents('secondFile.txt');

See Also:

Upvotes: 7

Ben Shelock
Ben Shelock

Reputation: 20975

file_get_contents is the most simple solution however curl is much more efficient. It's faster, more secure and more flexible.

function file_get_contents_curl($url){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

$page = file_get_contents_curl('demo.php');

Upvotes: 1

Matteo Riva
Matteo Riva

Reputation: 25060

What do you mean by "returns hello"?

If it really returns it as in

return "hello";

you can get the value simply like this:

$var = include 'demo.php'

if it echoes that value instead, you can read its output:

$var = file_get_contents("http://host/demo.php");

Upvotes: 2

Pascal MARTIN
Pascal MARTIN

Reputation: 401132

The text is returned when PHP interprets the page -- which means you have to either :

  • Run PHP from the command-line
  • Or call the page via your webserver -- which is what you'll generally tend to do.


In the second case, you need to send an HTTP request, and fetch the result, which can be done using file_get_contents (if the allow_url_fopen configuration directive is enabled) :

$content = file_get_contents('http://www.yoursite.com/demo.php');


Another solution, especially useful when allow_url_fopen is disabled, is to use curl ; see for instance the examples on the page of the curl_exec function.

Upvotes: 0

Related Questions