3

I'm setting up a SOAP server using php. In processing the SOAP requests, I will then need to call some existing python scripts. These scripts have typically returned 3 values: success, description, and data.

How do I pass all three values back to the php?

My request is:

exec('python test.py', $output);

test.py does:

from getData import getData

status, description, data = getData()

return status, description, data

and my existing python looks something like this:

def getData():
    database = Database()

    # get all the Data from the db
    data = database.getData()

    if data is None:
        return False, "...notes...", ""

    return True, "...notes...", data

I'm not sure the return in test.py is correct. But even if it is, when I look at var_dump($output) all I see is "Array"

Anyone have any ideas?

TIA,

Brian

2
  • What happens when you execute python test.py on the command line? Commented Jul 12, 2012 at 23:10
  • Good point. I did have print at first but changed to return. If I do a return I get "SyntaxError: 'return' outside function" Commented Jul 12, 2012 at 23:19

1 Answer 1

5

Since the exec() call receives shell output into the variable $output, it has no way of receiving the returned strings from the Python functions. Instead of returning them, you must print them, and each printed line will go into the exec() array $output.

The main script must print the values to stdout:

from getData import getData
status, description, data = getData()

# Print the values
print("STATUS:", status)
print("DESCRIPTION:", description)
print("DATA:", data)

In PHP, examine the output you get back:

exec("python test.py", $output);
var_dump($output);

You will need to format your print statements in Python into something you can easily parse values from in PHP, such as one thing per line, or key: value pairs that you can explode().

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

10 Comments

Ok, T/F would go to the exec's return_var then? If the "notes" that I listed in my example is a multi-line, variable length string, will I still be able to access this through $output[0] and the data in $output[1]?
@BrianCox Sorry, I had that wrong. Your call to getData() can work as you had it, but you need to print the values from the python script. If you are expecting multi-line strings, you will need to devise some method to parse them out, like a key: value pair.
If the output is going to be complex, I'd think about encoding the python output into JSON, which would make reading it in PHP a bit easier.
When I do (from the command line):python test_get_bcas.py I get: ('STATUS:', True, '\n') ('DESCRIPTION:', 'Success', '\n') ('DATA:', ['val1', 'val2'], '\n') Works as I would expect. But in the php, var_dump($output) gives me: array(0) { } Btw, thanks for the help with this. Python and PHP are both fairly new to me. I think I know just enough to be dangerous! :)
@ernie JSON may be worth looking into as well. Thank you both for the sugggestions!
|

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.