0

I'm trying to make this work on XAMPP. It works fine on a Linux server/virtual machine. I need to send an associative array to a python script, and I do it with:

<?php
 echo "Test simple call/answer process<br>";
 $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
 $param = base64_encode(json_encode($age));
 $command = escapeshellcmd('python python_array_answer.py $param');
 $output = shell_exec($command);
 $obj = json_decode($output);
 print_r($obj);
?>

The Python script is:

#! /Users/.../AppData/Local/Programs/Python/Python37/python.exe
import sys
import json
import base64
hotel_data = json.loads(base64.b64decode(sys.argv[1]))
print(json.dumps(hotel_data))

I get a NULL array back, with this error:

 Traceback (most recent call last):
 File "python_array_answer.py", line 6, in <module>
 hotel_data = json.loads(base64.b64decode(sys.argv[1]))
 File "C:\Users\mghiglia\AppData\Local\Programs\Python\Python37\lib\base64.py", line 87, in b64decode
 return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (5) cannot be 1 more than a multiple of 4
Any ideas? thank.
asked Nov 11, 2022 at 14:17

1 Answer 1

1

Assuming the rest of the code is okay, you're using single quotes in this line

$command = escapeshellcmd('python python_array_answer.py $param');

where you should either use double quotes, so php known to replace $param with the value of variable $param, or concatenate the two like this...

$command = escapeshellcmd('python python_array_answer.py '.$param);
answered Nov 11, 2022 at 14:37
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.