Passing a Python list to PHP - Only achievable with JSON?
Question: I've been doing some research around and haven't found a way to solve the
situation I'm faced with now.

I need to pass a Python list to PHP. I've been reading about doing it with
JSON but I was wondering if it was possible without it.

My list looks something like this:

    
    
    a_list = [0,['A1', 'A2', ['A3','A4']], ['B5', 'B2', ['B3','B4']]]
    

I have found how to pass simple values between Python and PHP but this is a
bit more complicated.

Also, I have found on another question asked here something that works for
dictionaries:

Python side:

    
    
    import json
    
    D = {'foo':1, 'baz': 2}
    
    print json.dumps(D)
    

PHP side:

    
    
    <?php 
    
    $result = json_decode(exec('python myscript.py'), true);
    echo $result['foo'];
    

Any help would be appreciated.

Thanks!


Answer: You'll need to serialize the data to somehow, to send it across. Here are a
couple of ways to do it:

  1. JSON
  2. Protobuf
  3. Write the data out to a text file in python and read it back in with PHP
  4. Start a server in your PHP program and have your python program write the data to it in some format over a socket

