php - Return value from include on a remote file -


i trying run script on remote server, , have results of script returned calling script. variable sent remote script, , based on remote script meant retrieve list of filenames on remote server, , return filenames array. however, using return in included file not returning actual value, aborts script. other that, remote script runs without problem, , can have var_dump list of filenames me, doesn't me on local script. both servers owned (us being company).

i've tried simple see if return value , 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). code of remote script works perfectly, i've tested, , variable send variable goes through no problem. problem not getting return value remote_script.

also, yes allow_url_include on local server. however, off remote server; should not make difference: http://php.net/allow-url-include.

i have looked on of other related questions on topic, , nothing seems quite describe problem. appreciated, have spent few hours looking on , have not made progress.

try using file_get_contents() instead of include.
writes file variable [causing script execute remotely], won't run response locally.

alternatively if have ability use curl, safer , quicker.
small snippet 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 response of curl request variable (instead of printing screen).
  • curlopt_followlocation has not been set, if page has been moved, curl not follow it. can set that, or have set based on second argument.
  • http_code, if above 400 (an err code, presumably 404 or 500) return false instead of fancy custom 404 page might setup.
    in testing, get_headers() more reliable curlinfo_http_code requires second call page being included, can make things go awry.
    eg. if (!strpos(200, get_headers($url)[0])) return false;

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -