1

I am using the following code to initiate a python script and pass a php variable to it.

$tmp = exec("python path/to/pythonfile.py $myVariable $mySecondVariable", $output);

This works very well, my issue is that I will need to pass 100+ variables to the python script. I don't want this exec line to become extremely long and unmanageable. I have also explored passing a php array instead of a variable with the following code:

$checked = array(
"key1"     => "1"
"key2"     => "1"
"key3"     => "1"
);
$checkedJson = json_encode($checked);
$tmp = exec("python path/to/pythonfile.py $myVariable $checkedJson", $output);

With this I have been unable to decode the JSON on the python side. I have been able to do a basic print of the array variable(undecoded) in python, but it gives every individual character as a new array value. ie [0] = k, [1] = e, [2] = y, [3] = 1, etc... Any help is greatly appreciated.

Just to be clear,I am looking for a simpler method than encoding and decoding an array. Is there a way I can format the exec line to allow for multiple variables.

1 Answer 1

1

Store your PHP variables within a temporary text file then use python to read that file.

Simple and effective.


Assuming Scripts are in the same directory

PHP Portion

long version (self contained script - skip to the short version below if you only want the code snippet)

<?php

#Establish an array with all parameters you'd like to pass. 
#Either fill it manually or with a loop, ie:

#Loop below creates 100 dummy variables with this pattern.  
#You'd need to come up with a way yourself to fill a single array to pass
#$variable1 = '1';
#$variable2 = '2';
#$variable3 = '3';
#....
#$variableN = 'N';
#...    
for ($i=1; $i<=100; $i++) {
    ${'variable'.$i} = $i;
}

#Create/Open a file and prepare it for writing
$tempFile = "temp.dat";
$fh = fopen($tempFile, 'w') or die("can't open file");

#let's say N=100
for ($i=1; $i<=100; $i++) {

    #for custom keys 
    $keyname = 'Key'.$i;

    # using a variable variable here to grab $variable1 ... $variable2 ... $variableN     ... $variable100
    $phpVariablesToPass[$keyname] = ${'variable'.$i} + 1000;

}

#phpVariablesToPass looks like this:
# [Key1] => 1001 [Key2] => 1002 [Key3] => 1003  [KeyN] = > (1000+N)


#now write to the file for each value.  
#You could modify the fwrite string to whatever you'd like
foreach ($phpVariablesToPass as $key=>$value) {
    fwrite($fh, $value."\n");
}


#close the file
fclose($fh);

?>


or in short, assuming $phpVariablesToPass is an array filled with your values:

#Create/Open a file and prepare it for writing
$tempFile = "temp.dat";
$fh = fopen($tempFile, 'w') or die("can't open file");
foreach ($phpVariablesToPass as $key=>$value) {
    fwrite($fh, $value."\n");
}
fclose($fh);


Python Snippet to Grab the Data

lines = [line.strip() for line in open('temp.dat')]

the variable lines now contains all of your php data as a python list.

Sign up to request clarification or add additional context in comments.

4 Comments

Let me know if you'd like an example on how to do this. I've moved countless large datasets between languages/scripts using temp files, and it really is the way to go.
Sure that would be helpful.
Hope those examples help. Make sure that temp.dat permissions are set so that php can open/write to the file and python can open/read the file. Mainly focus on the short version of the php for the snippet. The long version is a self contained script that you can use as a template.
Sorry for the delay, this is a bit of a side project at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.