I'm trying to pass variable from PHP file to Perl file by this code
In php file:
exec("perl delete_all.pl $used");
And in perl file I am trying: filename.pl
print $used;
But it's not working.
Perl isn't seeing $used, but the value of $used:
Get it from the argument list in Perl:
$used = $ARGV[0];
Related
I am looking for a way to obtain a Python list and display it on my website using PHP.
I've checked out and tried many online help-requests so I was hoping someone would be able to explain to me what it is I am doing wrong.
My Python script scrapes a website and puts the result in a Python list.
What I am trying to achieve is the following:
I want to display (a part) of the list on my website.
I've tried to accomplish this with the following code:
PHP
<?php
$outputArray = [];
$returnStatus;
exec('python ./scrapeWebsite.py', $outputArray, $returnStatus);
var_dump($outputArray);
echo $returnStatus;
?>
Python:
print(newsHeadlines) -> returning a list like this: ['item 1','item 2','item 3','item 4']
However, the array comes back as array(0) { } and the $returnStatus value is 1.
Encoding the list with JSON and writing it to a file on the disk, and then reading the file in PHP and decoding JSON did the trick. To ensure the web-data is up-to-date I'm using exec(); to execute the python file.
If going full Python this is an option you could use.
To help others with a similar problem, this is the code I used:
Python:
myListJson = json.dumps(myList) #Encode our Python list into a JSON string
f = open("./fileName.txt", "w") #Open the file that we want to write to for write access
f.write(myListJson) #Write the JSON String to the file that we have currently open
f.close() #Close the file
PHP:
exec(python3 /path/to/file/script.py); //Will execute the python script, ensure the needed packages are installed on the **SERVER**.
$fileContents = file_get_contents("fileName.txt");
$decodedJson = json_decode($fileContents); //This will now contain your Array like any other PHP Array.
Thanks for the help!
i have a php file which have some variables para1 and para2 , i want to send them to a python file .
this is my php file:
<?php
$para1 = "one";
$para2 = "two";
echo "shell_exec("
/C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project
/check.py '$para1' '$para2'")";
?>
and this is my py file:
import sys
x=sys.argv[1]
y=sys.argv[2]
print(x)
print(y)
but this does not work for me. Can someone please help me with this or suggest some other way?
Don't put quotes around the function call. That turns it into a literal string, not a call to the function. Windows pathnames don't use a / before the drive letter, so the path should start with C:. And there shouldn't be a space after Project.
echo shell_exec("C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project/check.py '$para1' '$para2'");
Also, if you're just going to echo the result, you can use the passthru function instead.
I had a similar problem. Solved it by adding the word python in front of the path to the python script.
As in:
echo shell_exec("python C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project/check.py '$para1' '$para2'");
I have a python script which has a python function inside it and function takes a parameter. Now i want to execute the python function. and the parameter is given from php.
But python file is in another directory.
Ex: php is in site/ph directory and python is in site/pyt directory
Please guys help me on executing the python from php
i appreciate your help
Consider passing the PHP parameter as a command line argument into Python using PHP's exec(). Then have Python receive it with its sys.argv list:
PHP Script (note: $output is an array and $return_var is a scalar, 0 or otherwise):
$arg = "argument 1";
exec('python "/site/pyt/Script.py" "'.$arg.'"', $output, $return_var);
// PYTHON OUTPUT
echo "return value is: $return_var" . "\n";
foreach ($output as $line) {
echo $line."\n";
}
Python Script (note: sys.argv is a list with filename of script as first argument)
import sys
# RECEIVING COMMAND LINE ARG
php_param = sys.argv[1]
def somefunction(p):
print("PHP Paramter: {}".format(p))
somefunction(php_param)
Output (when PHP script is run)
# return value is: 0
# PHP Paramter: argument 1
I want to pass a text as a argument from a bash file to a php script like this:
bash script
#!/bin/sh
php /var/www/html/assets/sms/get_sms.php $SMS_1_NUMBER $SMS_1_TEXT
php script
<?php
$url = "http://localhost/user/user/get_sms/".$argv[1];
$postdata = array('number' => $argv[1],'text'=>$argv[2]);
do_post_request($url,$postdata);
function do_post_request($url, $postdata)
{
//My function
}
?>
The problem is that, the first argument to the bash file is a number but the second argument is text. The Php file which receives the arguments just takes the first string of the text. For example, if the text of the $SMS_1_TEXT variable is "How can I make it work" , the php file will receive only "How".
How can I make it woks better?
Thank you very much
Quoting. Replace $SMS_1_TEXT by "$SMS_1_TEXT".
I am trying to run a .exe application with input file and argument.
With cmd I can successfully start the executable like this...
C:\Program Files\MyApp.exe "path\to\input file" argument
However, nothing happens when I simply copy paste the string above into the exec() function like this..
exec("C:\Program Files\MyApp.exe "path\to\input file" argument")
Do I need to escape parts of the string? How should I proceed?
Just pass the arguments like a normal calling from shell
ex:
exec("C:\Program Files\MyApp.exe \"path to\input file\" argument")
I had to use this format
php -q "./yii.php" migrate/up --interactive=0 --migrationPath=#vendor/pheme/yii2-settings/migrations