1

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

Shahroz
  • 11
  • 3
  • 1
    `doesn't work`...means what? Is there is a specific error or problem? Is the problem in the PHP code or the python code? is the python code designed to deal with these strings, or does it only expect integers? You didn't show the python code or explain what it does. Nor did you show us any sample of the string-based data. Please provide a [mre] of the issue. See also [ask]. Thanks. – ADyson Apr 01 '23 at 00:15
  • Hi, thank you for your comment, I have made some changes. Hopefully this is all you need, thank in advance! – Shahroz Apr 02 '23 at 05:10
  • Thanks. One more thing...if you test the python script independently and pass string into it (e.g. by running it yourself from the command line), does it work then? – ADyson Apr 02 '23 at 11:05
  • When I change the input in the python code to strings, the code prints the strings as it's supposed to. – Shahroz Apr 02 '23 at 19:13
  • This then suggests that maybe `escapeshellarg($input)` isn't producing the output you expected. Have you tested that separately to see what command it results in? – ADyson Apr 02 '23 at 21:30
  • Removing the `escapeshellarg()` argument doesn't change anything in the output. With the array of integers, it still works and with the array of strings, it still doesn't. I eventually want to send inputs in a html form to a python file. If you know a different way of achieving it, feel free to suggest it. – Shahroz Apr 02 '23 at 23:29
  • I didn't say to remove it, I said to test what it produces, and compare it to the version you wrote yourself, by hand. I would assume it's different in some way. – ADyson Apr 03 '23 at 09:28
  • e.g. you can compare the outputs of the integer version and string version of the command in this demo: https://3v4l.org/pF4DU . How does the string version compare to what you typed by hand? (I am not a python user so I don't know necessary how it needs to be provided). – ADyson Apr 03 '23 at 12:08

0 Answers0