Assigning variable from remote script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ggDuff
Forum Newbie
Posts: 3
Joined: Tue Jul 22, 2003 3:47 pm

Assigning variable from remote script

Post by ggDuff »

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!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

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.

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);
?>
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? :D
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

...

Post by kettle_drum »

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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Re: ...

Post by Gen-ik »

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.
... 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.

If the previous script I posted works you may as well just use that.
ggDuff
Forum Newbie
Posts: 3
Joined: Tue Jul 22, 2003 3:47 pm

Post by ggDuff »

There are two other options that have been given to me on my ISP's forum:
PHP:--------------------------------------------------------------------------------
$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");

--------------------------------------------------------------------------------
AND
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();

--------------------------------------------------------------------------------
Of all these options, which would be the best?

Thanks!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Using implode() is designed to convert an array into a single string and place X character between the values for example..

Code: Select all

<?php
$a = Array('apple','banana','cherry');
$result = implode("*", $a);
?>
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...

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); 
?>
...have you tried it yet??
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

When you call implode() with only 1 argument, it basically does this:

Code: Select all

function implode($array){
foreach ($array as $value){
$string.=$value;
}
return $string;
}
So you can use implode() to concatenate all of an array's values together with no separators, if you use only 1 argument.
ggDuff
Forum Newbie
Posts: 3
Joined: Tue Jul 22, 2003 3:47 pm

Post by ggDuff »

Gen-ik wrote: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); 
?>
...have you tried it yet??
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!!
Post Reply