I am trying to get some data in a php file to a python file in the JSON format. Right now everything works like it's supposed to when I use an array of integers, but when I change the array to an array of strings (uncommenting line 3 below and commenting line 2), the code doesn't work anymore. I eventually want to use data from a form, but that isn't possible right now.
<?php
$input = array(1,2,3);
//$ input = array('test', 'test');
$input = json_encode($input);
$output = exec("form.py ". escapeshellarg($input));
var_dump($output);
echo "<p>Output from Python: <br>" . $output . "<p>";
echo "Dump the output from Python as JSON<br>";
var_dump(json_decode($output));
$data = json_decode($output);
echo "Accessing from JSON array:<br>";
foreach($data as $key=>$value) {
echo "[".$key . "] => " . $value . "<br>";
}
?>
This is de python code used:
import sys
import json
output = []
output.append("Values submitted: " + str(sys.argv))
j = json.loads(sys.argv[1])
for k in j:
output.append("Return from json: " + str(k))
print(json.dumps(output))
This is de error I get to an array of strings:
string(0) "" Output from Python:
Dump the output from Python as JSON NULL Accessing from JSON array:
Warning: foreach() argument must be of type array|object, null given in C:\xampp\htdocs\test\form_action.php on line 11