11

I'd like to be able to pass a PHP array to a Python script, which will utilize the data to perform some tasks. I wanted to try to execute my the Python script from PHP using shell_exec() and pass the JSON data to it (which I'm completely new to).

$foods = array("pizza", "french fries");
$result = shell_exec('python ./input.py ' . escapeshellarg(json_encode($foods)));
echo $result;

The "escapeshellarg(json_encode($foods)))" function seems to hand off my array as the following to the Python script (I get this value if I 'echo' the function:

'["pizza","french fries"]'

Then inside the Python script:

import sys, json
data = json.loads(sys.argv[1])
foods = json.dumps(data)
print(foods)

This prints out the following to the browser:

["pizza", "french fries"]

This is a plain old string, not a list. My question is, how can I best treat this data like a list, or some kind of data structure which I can iterate through with the "," as a delimiter? I don't really want to output the text to the browser, I just want to be able to break down the list into pieces and insert them into a text file on the filesystem.

1
  • In your example (data = json.loads(sys.argv[1])) data is already a list, then with dumps you are converting it back again to a string Commented Oct 24, 2017 at 8:27

4 Answers 4

11

Had the same problem

Let me show you what I did

PHP :

base64_encode(json_encode($bodyData))

then

json_decode(shell_exec('python ' . base64_encode(json_encode($bodyData)) );

and in Python I have

import base64

and

content = json.loads(base64.b64decode(sys.argv[1]))

as Em L already mentioned :)

It works for me Cheers!

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

2 Comments

It worked with multidimensional array in PHP to python.
i hope this works cross platform
1

You can base64 foods to string, then passed to the data to Python and decode it.For example:

import sys, base64
if len(sys.argv) > 1:
    data = base64.b64decode(sys.argv[1])
    foods = data.split(',')
    print(foods)

Comments

0

If you have the json string: data = '["pizza","french fries"]' and json.loads(data) isn't working (which it should), then you can use: MyPythonList = eval(data). eval takes a string and converts it to a python object

Comments

0

Was having problems passing json from PHP to Python, my problem was with escaping the json string, which you are doing. But looks like you were decoding then re-encoding with "food"

From what I understand

Python json.dumps(data) == PHP json_encode(data)

Python json.loads(data) == PHP json_decode(data)

json.loads(data) -> String Data

json.load(data) -> File Data

json.dumps(data) -> String Data

json.dump(data) -> File Data

PHP:

$foods = array("pizza", "french fries");
$result = shell_exec('python ./input.py ' . escapeshellarg(json_encode($foods)));
echo $result;

Python:

data = json.loads(sys.argv[1])
for v in data:
   print(v)

ALSO

if you are passing key:value

PHP:

$foods = array("food1":"pizza", "food2":""french fries");
$result = shell_exec('python ./input.py ' . escapeshellarg(json_encode($foods)));
echo $result;

Python:

data = json.loads(sys.argv[1])
for(k,v) in content2.items():
   print("k+" => "+v)

Python:

data = json.loads(sys.argv[1])
print(data['food1'])

1 Comment

works nicely on mac and linux, but escapeshellarg behave really differently on windows, thus cannot work on this platform :/

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.