LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to pass variables to perl script from php script (https://www.linuxquestions.org/questions/programming-9/how-to-pass-variables-to-perl-script-from-php-script-813540/)

smohan 06-11-2010 07:23 AM

how to pass variables to perl script from php script
 
Hi,
Im using perl script from php. I wanted to pass parameters to perl script from php and also return value from perl script to php. I dont no how to do it, Please some one help me in solving this problem.


Thanking you in Advance.

nuwen52 06-11-2010 07:50 AM

Well, what type of values are you passing? Are they easily represented as command line arguments? Passing them back can be tricky unless it's a simple "return code" type value. If so, that should be moderately simple. You could also set environment variables if the types aren't complex.

TB0ne 06-11-2010 08:21 AM

Quote:

Originally Posted by smohan (Post 4000140)
Hi,
Im using perl script from php. I wanted to pass parameters to perl script from php and also return value from perl script to php. I dont no how to do it, Please some one help me in solving this problem.

Thanking you in Advance.

Have you bothered to check the PHP docs?? Specifically, the system, exec, and passthru directives?

http://php.net

And if you'd like detailed help, ask a detailed question. You say nothing about version/distro of Linux, post any of the code, or give any examples.

smohan 06-14-2010 01:38 AM

I thank both TB0ne and nuwen52, for answering my thread.

Ill give an example, to make you people understand my problem.

Here's a perl script, "test1.pl".
Code:

print "Hello from perl! "
And this is the php Script, test.php.

Code:

<?php

print "Hello from PHP! ";
$perl = new Perl();
$perl->require("test1.pl");
print "Bye! ";

?>

If I run test.php in the browser. The output is
Quote:

Hello from PHP!Hello fro perl!
This example works absolutely fine.

My question is, Can I pass variables or parameters to the perl script above from php script and return result from the perl script to php script?

If s, How can I do so.

Thanking you In Advance.

pixellany 06-14-2010 06:15 AM

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

smohan 06-14-2010 07:14 AM

Thank you pixellany, and please help me in solving the problem.

graemef 06-14-2010 08:02 AM

You example looks very much like to one given in this tutorial. If you continue through the tutorial you should learn how to achieve what you want to do.

smohan 06-14-2010 08:31 AM

Thank you graemef, yes I have used that example. And in that whole site, I did not get how to return values from the perl script, which should be used in php.

This is my perl script
Code:

#!/usr/bin/perl

# define subroutine
sub makeJuice
{
# retrieve the argument
my ($flavor) = shift(@_);

# and use it
#print "Making $flavor juice...\n";
return $flavor;
}
#my $tmp=@ARGV[0];
my $t=makeJuice("kumar","linga","mohan");
print $t,"\n";

This is my php script.


Code:

<?php
$output=shell_exec('perl 21.pl annaas');
print "this is ".$output." is the output";
?>

This is the output I'm getting

Quote:

this is HASH(0x8e5d880) is the output

The value HASH(0x8e5d880) is being print just because of the line16
Code:

print $t,"\n";
If I comment this line I'm getting this output
Quote:

this is is the output
Please help me solving this problem.

graemef 06-14-2010 09:07 AM

As the tutorial states you can use output buffering to capture as the output anything that is printed by the perl script. (example 2) You can establish a function and pass it values (example 6).

Try those out, see what you get.

smohan 06-15-2010 12:48 AM

Thank you graemef, I solved the problem by using output buffer. I'm storing the output(values) of the perl script in the buffer, and using these values in the php script.


Thank you very much for showing concern on me.

graemef 06-15-2010 01:20 AM

try printing the result within perl. Then using the output buffer technique capture the result in PHP

smohan 06-15-2010 05:12 AM

Thank you graemef. I'm using the same method. But I have another problem.

This is my perl script "code1.pl"

Code:

#!/usr/bin/perl -w
# --
# otrs.printUser - Prints some user data
# Copyright (C) 2001-2010 OTRS AG, http://otrs.org/
# --
# $Id$
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";
use lib "/opt/otrs/";

use Getopt::Std;
use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::DB;
use Kernel::System::Time;
use Kernel::System::User;

open OUTPUT, ">output.txt";


# create common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject);
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject} =  Kernel::System::Log->new( %CommonObject, LogPrefix => 'otrs.printUser', );
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject}  = Kernel::System::DB->new(%CommonObject);
$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);


my $User =@ARGV[0];
# fetch the data for the user
my %User = $CommonObject{UserObject}->GetUserData( User => $User, );


if ( !%User ) {
    print OUTPUT "User $User does not exist.\n";
    exit 1;
}


print OUTPUT "User:\t\t$User\n";
print OUTPUT "First name:\t$User{UserFirstname}\n";
print OUTPUT "Last name:\t$User{UserLastname}\n";
print OUTPUT "Email:\t\t$User{UserEmail}\n";
close OUTPUT;


This is my PHP file, which i run via browser.

Code:

<?php

$User="root@localhost";

shell_exec('perl code1.pl root@localhost');
$fh = fopen("output.txt", 'r');
$usr = fgets($fh);
$nam = fgets($fh);
fclose($fh);
print "$usr<br>";
echo "$nam <br>";

?>

If i run this code I'm getting the output
Quote:

User: root@localhost
First name: Admin
And if I use this code in the php script at line 5
Code:

shell_exec('perl code1.pl $User');
I'm getting the output as,
Quote:

User does not exist.
The problem is I'm not able to pass variables by reference($User), I should be calling it by value("root@localhost").
But I wanted to pass by reference ie.,$User.

graemef 06-15-2010 07:08 AM

If you use single quote then variables will not be expanded. Try using double quotes.
Code:

shell_exec("perl code1.pl $User");

smohan 06-15-2010 07:20 AM

Thank you graemef.

IT WORKED.

Thanks a lot......

smohan 06-16-2010 06:30 AM

Hi,

Im back with another problem.


I'm Using this HTML file, "adduser.html"
Code:

<html>
<head>
<script language="php">
session_start();
</script>
<title>login page</title>

</head>

<body bgcolor="#d8d8d8">


<div align=center>
<form action="adduser.php" method= "post"  >

<h1> welcome to cogitate</h1>


Source      :<input type="text" name="var1" size=30><br>
First Name  :<input type="text" name="var2" size=30><br>
Last Name  :<input type="text" name="var3" size=30><br>
CustomerID  :<input type="text" name="var4" size=30><br>
LoginName  :<input type="text" name="var5" size=30><br>
Password    :<input type="password" name="var6" size=30><br>
Email      :<input type="text" name="var7" size=30><br>
ValidID    :<input type="text" name="var8" size=30><br>
UserID      :<input type="text" name="var9" size=30><br>

<input type="submit" value="submit" >

<input type="reset">

</form>

</div>


</body>

</html>

I'm accepting the Values from the form to the php script, this is the php script "adduser.php"
Code:

<?php

        $Source = htmlspecialchars($_POST["var1"]);
        $UserFirstname  = htmlspecialchars($_POST["var2"]);
        $UserLastname  = htmlspecialchars($_POST["var3"]);
        $UserCustomerID = htmlspecialchars($_POST["var4"]);
        $UserLogin      = htmlspecialchars($_POST["var5"]);
        $UserPassword  = htmlspecialchars($_POST["var6"]);
        $UserEmail      = htmlspecialchars($_POST["var7"]);
        $ValidID        = htmlspecialchars($_POST["var8"]);
        $UserID        = htmlspecialchars($_POST["var9"]);

$ar=array("$Source", "$UserFirstname", "$UserLastname", "$UserCustomerID", "$UserLogin", "$UserPassword", "$UserEmail", "$ValidID", "$UserID");
$separated = implode(":", $ar);
passthru("perl adduser.pl $separated");

?>

and the perl script "adduser.pl"
Code:

#!/usr/bin/perl -w
# --
# otrs.printUser - Prints some user data
# Copyright (C) 2001-2010 OTRS AG, http://otrs.org/
# --
# $Id$
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;

#use \.\./ as lib location;
use lib "/opt/otrs/Kernel/cpan-lib";
#use File::Basename;
#use FindBin qw($Bin);
#FindBin::again();
#use lib dirname($Bin);
#use lib dirname($Bin) . "/Kernel/cpan-lib";
#use lib dirname($RealBin) . "/Kernel/cpan-lib/MIME";
use lib "/opt/otrs";
use MIME::Entity;
use Mail::Address;
use Getopt::Std;
use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::DB;
use Kernel::System::Time;
use Kernel::System::User;
use Kernel::System::Ticket;
use File::Path;
use Kernel::System::Ticket::Article;
use Kernel::System::Type;
use Kernel::System::State;
use Kernel::System::Priority;
use Kernel::System::Service;
use Kernel::System::SLA;
use Kernel::System::Lock;
use Kernel::System::Queue;
use Kernel::System::User;
use Kernel::System::Group;
use Kernel::System::Cache;
use Kernel::System::CustomerUser;
use Kernel::System::CustomerGroup;
use Kernel::System::Email;
use Kernel::System::PostMaster::LoopProtection;
use Kernel::System::TemplateGenerator;
use Kernel::System::LinkObject;
use Kernel::System::Valid;
use Kernel::System::HTMLUtils;
use Kernel::System::Crypt;


my %Opts = ();
getopt( 'h', \%Opts );
if ( $Opts{h} || !$ARGV[0]) {
    print "otrs.printUser - print some user details\n";
    print "Copyright (C) 2001-2010 OTRS AG, http://otrs.org/\n";
    print "usage: otrs.printUser [username] \n";
    exit 1;
}

# create common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject);
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject} =  Kernel::System::Log->new( %CommonObject, LogPrefix => 'otrs.printUser', );
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject}  = Kernel::System::DB->new(%CommonObject);
$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);
$CommonObject{SendMailObject} = Kernel::System::Email->new(%CommonObject);
$CommonObject{TicketObject} = Kernel::System::Ticket->new(%CommonObject);
$CommonObject{CustomerUserObject} = Kernel::System::CustomerUser->new(%CommonObject);


my $PHPstring=$ARGV[0];
my @token= split(/:/, $PHPstring);
print "@token[0]<br>@token[1]<br>@token[2]<br>@token[3]<br>@token[4]<br>@token[5]<br>@token[6]<br>@token[7]";
print "UserLogin\n";

my $UserLogin = $CommonObject{CustomerUserObject}->CustomerUserAdd(

        Source        => @token[0],
        UserFirstname  => @token[1],
        UserLastname  => @token[2],
        UserCustomerID => @token[3],
        UserLogin      => @token[4],
        UserPassword  => @token[5],
        UserEmail      => @token[6],
        ValidID        => @token[7],
        UserID        => @token[8],
    );
print "UserLogin\n";

if($UserLogin){
print "user added successfully";
}
else
{
print "adding failed";
}

exit 0;


And the problem I'm facing is,

I'm able to pass the values from html to php and from php to perl intern, but the Control is not passing into the perl script at line 98 and beyond.


But, if run the same php script from command prompt, the control is being passed to the line 98 and beyond.

Any solution would be appreciated.

Thanking you in Advance.


All times are GMT -5. The time now is 08:13 PM.