Meir
Meir

Reputation: 58

Return value from include on a remote file

I am trying to run a script on a remote server, and have the results of that script returned to the calling script. A variable is sent to the remote script, and based on that the remote script is meant to retrieve a list of filenames on the remote server, and return those filenames as an array. However, using return in the included file is not returning an actual value, it just aborts the script. Other than that, the remote script runs without a problem, and I can have it var_dump the list of filenames for me, but that doesn't do much good for me on the local script. Both servers are owned by us (us being my company).

I've tried something simple like this just to see if I could get a return value and it didn't work:

Local Script:

$test = include "http://remote_host_address/remote_script.php";  
var_dump($test);  

Remote Script:

$ret = "Hello World";
return $ret;

This outputs int(1). The code itself of the remote script works perfectly, that I've tested, and the variable I send as a get variable also goes through no problem. The only problem is that I am not getting a return value from the remote_script.

Also, yes allow_url_include is on for the local server. However, it is off for the remote server; but that should not make a difference: http://php.net/allow-url-include.

I have looked over some of the other related questions on this topic, and nothing seems to quite describe my problem. Any help would be greatly appreciated, as I have spent a few hours looking this over already and have not made any progress.

Upvotes: 0

Views: 2501

Answers (2)

SamGoody
SamGoody

Reputation: 14468

Try using file_get_contents() instead of include.
This writes the file into a variable [causing the script to execute remotely], but won't run the response locally.

Alternatively If you have the ability to use cURL, it is safer and probably quicker.
A small snippet to remotely file_get_contents();

function curl_get_contents($url){
    $c = curl_init($url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $res = curl_exec($c);
    if (curl_getinfo($c, CURLINFO_HTTP_CODE) > 400) $res = false;
    curl_close($c);
    return $res;
    }

By way of explanation

  • RETURNTRANSFER puts the response of the curl request into a variable (instead of printing to screen).
  • CURLOPT_FOLLOWLOCATION has not been set, so if the page has been moved, curl will not follow it. You can set that, or have it set based on a second argument.
  • HTTP_CODE, if above 400 (an err code, presumably 404 or 500) will return false instead of the fancy custom 404 page that might be setup.
    In my testing, get_headers() is more reliable than curlinfo_http_code but requires a second call to the page being included, which can make things go awry.
    eg. if (!strpos(200, get_headers($url)[0])) return false;

Upvotes: 3

Glavić
Glavić

Reputation: 43552

Script on other server http://remote_host_address/remote_script.php is probably being executed. Rename file to .txt and then use include on that file.


If the script must be run remotely, then run it, and return/echo php code. Example:

File: http://localhost/test.php

<?php
header('Content-Type: text/plain; charset=utf-8');

$array = array();
// your logic
$array['remote_server_time'] = time();
$array['sub'] = array(1, 2, 3);
// etc.

//output
echo '<?php return ' . var_export($array, true) . ';';

will output:

<?php return array (
  'remote_server_time' => 1381401586,
  'sub' => 
  array (
    0 => 1,
    1 => 2,
    2 => 3,
  ),
);

File: http://localhost/index.php

<?php
header('Content-Type: text/plain; charset=utf-8');

$array = include('http://localhost/test.php');
print_r($array);

will output:

Array
(
    [remote_server_time] => 1381401701
    [sub] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
)

Upvotes: 0

Related Questions