Page 1 of 1

Pass Array From Remote Function

Posted: Wed Oct 06, 2010 10:37 am
by djlex1928
Hi there, first I'll show you the two files.

Here's a file located on a remote server (on my network) -
blah.php

Code: Select all

<?php
$blah = array(
    'test1' => 'test3',
    'test3' => 'test4',
);

function test() {
return $blah;
}
?>
And here's what's on my local server -
test.php

Code: Select all

<?php
include('http://192.168.0.8/blah.php');
echo test();
?>
As you've probably guessed, this returns nothing and tells me the function doesn't exist. Is there any way in php to include this remote file and return the array from the remote file to a local array, allowing me to use the contents of the array in a local script?

EDIT - Make sure you expand the first PHP code, the function "test" is in there.

Re: Pass Array From Remote Function

Posted: Wed Oct 06, 2010 11:29 am
by internet-solution
Can you post the exact error message you are getting?

The test() should be available to local server. However

Code: Select all

echo test();
will be blank as $blah is not initialised (it is outside the scope of test() function).

Also you need to remember that when you use include in PHP, it does not execute the remote function, it just adds the remote code to the current file.

Re: Pass Array From Remote Function

Posted: Wed Oct 06, 2010 12:50 pm
by twinedev
Also, what you are including is the OUTPUT of blah.php. When you server makes the call over to blah.php, that server runs the script.

Form the docs:
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper - see List of Supported Protocols/Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

Re: Pass Array From Remote Function

Posted: Wed Oct 06, 2010 1:34 pm
by Monotoko
Try this instead:

Code: Select all

echo blah;

Re: Pass Array From Remote Function

Posted: Wed Oct 06, 2010 2:28 pm
by CowBoySD
Hello,


In your function, you add "global" + variable $blah :wink:

Code: Select all

function test() {
global $blah;
return $blah;
}

@++
CowBoySD

Re: Pass Array From Remote Function

Posted: Wed Oct 06, 2010 4:42 pm
by twinedev
It is still missing the point that test.php will NOT know that the function test() exists. it is not defined anywhere in test.php and when you include blah.php it is the same as including a local file that is blank, as it does NOT output any code for test.php to include.

blah.php executes on its server, not on the server with test.php.

-Greg

Re: Pass Array From Remote Function

Posted: Thu Oct 07, 2010 12:24 am
by CowBoySD
Hello,

Ah yes, exactly ...
Well ... ben you can then rename your external page in .txt and you parses this page in your script php. But in this case, everyone can see this page .txt ;)