passing parameters to python script via php
So obviously you can't drop an array into a string and expect output, but similarly you shouldn't just drop a string into a command either. You need to escape your values to prevent possible problems from arising due to special characters, whitespace etc.
Just run through the array and build a command line string like this:
$item = array(
'--native-libs-dir' => "/home/gdqupqmy/quikklyLib/",
'--type' => "template0001style1",
'--data'=> "12345",
'--join' => "horizontal",
'--output-file' => "/home/gdqupqmy/quikklyLib/saavr-12345.svg"
);
$args = "";
foreach ($item as $k=>$v) {
$args .= " $k " . escapeshellarg($v);
}
$cmd = "/usr/bin/python /full/path/to/quikkly-generate-code.py $args";
$result = exec($cmd, $output, $return);
printf("Command exited with code %d, full output follows: %s", $return, print_r($output,1));
This ensures all values are quoted and escaped. (I've made the assumption that the array keys are fixed values and so are not dangerous, if this is not the case they can be run through escapeshellarg
as well.)
passing php variable to python file
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.
Related Topics
Cannot Use Concatenation When Declaring Default Class Properties in PHP
Converting Named HTML Entities to Numeric HTML Entities
How to Save and Retrieve Contenteditable Data
Should I Mix Angularjs with a PHP Framework
Changing the Add to Cart Button Text in Woocommerce for Items with Variations
Query Time Result in MySQL W/ PHP
How to Flush Data to Browser But Continue Executing
Detect "Overall Average" Color of the Picture
Read Associative Array from JSON in $_Post
How to Get Google +1 Count for Current Page in PHP
Convert a JSON into a Utf-8 String
How to Get PHPunit Mockobjects to Return Different Values Based on a Parameter
How Is MVC Supposed to Work in Codeigniter
Pass PHP Variable to Bootstrap Modal
Using Preg_Split with Multiple Spaces
How to Post a Large Amount of Data Within PHP Curl Without Memory Overhead
How to Add Additional PHP Versions to Mamp
Folder Structure of a PHP MVC Framework... am I Doing This Right