0

HTML

This is a form that accepts a user's input (url):

<form method="post" action="/" accept-charset="utf-8">
    <input type="search" name="url" placeholder="Enter a url" />
    <button>Go</button>
</form>

PHP (Laravel)

This controller stores the value of the user's input (url) into a variable for use in the python script.

$url = Input::get('url');
$name = shell_exec('path/to/python ' . base_path() . '/test.py ' . $url);
return $name;

Python

The script passes what the user input and takes the text after a forward slash and displays it.

url = sys.argv[1]
name = url.split('/')[-1]
print(name)

Going back to the PHP, it returns the value of what was executed in the Python script. If a user inputs the url: http://example.com/file.png it will successfully return the string file.png

Everything about this works, but I'm realizing that I'm limiting myself, since the shell_exec command is only going to paste out the string that is returned from the python script. What do I need to do if I want to have multiple variables returned?

Python in question

url = sys.argv[1]
name = url.split('/')[-1]
test = "pass this text too!"
# ? what goes here ?

So the HTML page should now return file.png and pass this text too!

Am I going to need to return an array or json in the python and then extract the array/json in PHP?

NOTE: I am aware of security flaws/injections in these examples, these are stripped down versions of my actual code.

4
  • Not quite an answer to your stated question, but if this is a project of more than minor complexity, I'd recommend using a database to handle communication between the various processes. SQLite has very little overhead and can work wonders if everything is on one machine. Commented Jan 8, 2015 at 20:53
  • @KalZekdor can you elaborate more on what you mean? What would the db store? Commented Jan 8, 2015 at 22:34
  • Depends on your particular application. Eg, if a user is uploading an image via php which needs to be used by python, instead of passing the path to the image as a python arg, store the image (path or data), uploader, upload date, status of the job, etc. The python script could then run through unprocessed jobs in the db and act on them. Commented Jan 14, 2015 at 7:44
  • As I said, it's not exactly an answer to what you're asking, but it's worth looking into (unless this is for a quick one-off script, in which case it's a bit overkill). You may be able to structure your app in a more robust manner. Passing data back and forth via command line arguments is a quick and dirty solution; easy to implement, but prone to breakage, and a maintenance nightmare. Commented Jan 14, 2015 at 7:50

2 Answers 2

2

You can return a an array, but be sure that elements don't contain the delimiter. Like print(','.join(name, test)). Or you can encode to json like json.dumps([name, test]), then parse json in PHP. Second one is better, of course.

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

2 Comments

i have voted up, because i missed JSON !! and i work with it every day. shame on me :D
@HalayemAnis shame on you :D
0

In this case, your Python script has to return a formatted response, that PHP script can parse properly and determine variables with their values
for example, your python code has to return something like that :

file_name=file.png;file_extension=png;creation_date=1/1/2015;

After that in your php code yo do the necessary stuff

$varValues = explode(";", $PythonResult);
foreach($varValues as $vv) {
    $temp = explode("=", $vv);
    print 'Parameter : ' . $temp[0] . ' value : ' . $temp[1] . '<br />';
}

Comments

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.