Assigning variable from remote script
Moderator: General Moderators
Assigning variable from remote script
I have a php script that needs to assign a variable from a remote perl script.
I tried this:
$a = include('http://www.remotedomain.com/cgi-bin/inventory_check.pl');
but it only gave me a value of true (1)
The perl script outputs only a simple integer - no HTML or anything else.
This seems like such a simple thing to do and I have a feeling I'm gonna feel pretty dumb when I find the answer %) but I haven't been able to figure it out thus far.
Thanks in advance!
I tried this:
$a = include('http://www.remotedomain.com/cgi-bin/inventory_check.pl');
but it only gave me a value of true (1)
The perl script outputs only a simple integer - no HTML or anything else.
This seems like such a simple thing to do and I have a feeling I'm gonna feel pretty dumb when I find the answer %) but I haven't been able to figure it out thus far.
Thanks in advance!
The reason you are getting 'true' when using that line of code is simply because PHP is telling you that the file has been included succesfully. It's a bit like using $is_variable_set = isset($var)..... which will result in $is_variable_set being 1, or true, if the variable $var exists.
I guess you could get the variable from the perl script by loading the file using fopen().
I'm not sure if the following will work because I'm writing it as I go but it will give you a good idea of what to do..... hopefully.
I don't know anything about perl but I'm guessing that it's a server-side script like PHP.. ie, it gets executed before being dumped into the users browser. If it isn't then this might not work as expected but it's worth a shot.
Hope this helps?
I guess you could get the variable from the perl script by loading the file using fopen().
I'm not sure if the following will work because I'm writing it as I go but it will give you a good idea of what to do..... hopefully.
Code: Select all
<?php
$file = "http://www.remotedomain.com/cgi-bin/inventory_check.pl";
$connection = fopen($file, "r");
$data = fread($connection, 32000);
fclose($connection);
echo("the variable from my perl script is : ".$data);
?>Hope this helps?
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
...
Try passthru();
http://uk2.php.net/manual/en/function.passthru.php
This should allow you to execute an external file and will return the output of it.
http://uk2.php.net/manual/en/function.passthru.php
This should allow you to execute an external file and will return the output of it.
Re: ...
... this is really designed to be used with external programs and utilities and has a lot of down-sides to it including one hellofa lot of buffering.kettle_drum wrote:Try passthru();
http://uk2.php.net/manual/en/function.passthru.php
This should allow you to execute an external file and will return the output of it.
If the previous script I posted works you may as well just use that.
There are two other options that have been given to me on my ISP's forum:
Thanks!
ANDPHP:--------------------------------------------------------------------------------
$a = implode(file("http://www.remotedomain.com/cgi-bin/inventory_check.pl"));
--------------------------------------------------------------------------------
Alternatively if you have PHP Version 4.3.0 or newer, then you could use this:
PHP:--------------------------------------------------------------------------------
$a = file_get_contents("http://www.remotedomain.com/cgi-bin/inventory_check.pl");
--------------------------------------------------------------------------------
Of all these options, which would be the best?Use the output buffer functions:
PHP:
--------------------------------------------------------------------------------
ob_start();
include("http://www.remotedomain.com/cgi-bin/inventory_check.pl");
$a = ob_get_contents();
ob_end_clean();
--------------------------------------------------------------------------------
Thanks!
Using implode() is designed to convert an array into a single string and place X character between the values for example..
would result in $result = "apple*banana*cherry" so I can't see how that is going to work. You really don't need to use any buffering for this simple thing you are trying to do.
If the following code works then it's your best bet I think...
...have you tried it yet??
Code: Select all
<?php
$a = Array('apple','banana','cherry');
$result = implode("*", $a);
?>If the following code works then it's your best bet I think...
Code: Select all
<?php
$file = "http://www.remotedomain.com/cgi-bin/inventory_check.pl";
$connection = fopen($file, "r");
$data = fread($connection, 32000);
fclose($connection);
echo("the variable from my perl script is : ".$data);
?>When you call implode() with only 1 argument, it basically does this:
So you can use implode() to concatenate all of an array's values together with no separators, if you use only 1 argument.
Code: Select all
function implode($array){
foreach ($array as $value){
$string.=$value;
}
return $string;
}No - I tried the first suggestion that was made which was to use ob - and it works - but then I was told that it could cause problems with cookies, which is why I'm looking for a better way. I will try the method above. Thanks!!Gen-ik wrote:If the following code works then it's your best bet I think...
...have you tried it yet??Code: Select all
<?php $file = "http://www.remotedomain.com/cgi-bin/inventory_check.pl"; $connection = fopen($file, "r"); $data = fread($connection, 32000); fclose($connection); echo("the variable from my perl script is : ".$data); ?>