executing Python script in PHP and exchanging data between the two

Question:

Is it possible to run a Python script within PHP and transferring variables from each other ?

I have a class that scraps websites for data in a certain global way. i want to make it go a lot more specific and already have pythons scripts specific to several website.

I am looking for a way to incorporate those inside my class.

Is safe and reliable data transfer between the two even possible ? if so how difficult it is to get something like that going ?

Asked By: Neta Meta

||

Answers:

The best bet is running python as a subprocess and capturing its output, then parsing it.

$pythonoutput = `/usr/bin/env python pythoncode.py`;

Using JSON would probably help make it easy to both produce and parse in both languages, since it’s standard and both languages support it (well, at least non-ancient versions do). In Python,

json.dumps(stuff)

and then in PHP

$stuff = json_decode($pythonoutput);

You could also explicitly save the data as files, or use sockets, or have many different ways to make this more efficient (and more complicated) depending on the exact scenario you need, but this is the simplest.

Answered By: Amadan

You can generally communicate between languages by using common language formats, and using stdin and stdout to communicate the data.

Example with PHP/Python using a shell argument to send the initial data via JSON

PHP:

// This is the data you want to pass to Python
$data = array('as', 'df', 'gh');

// Execute the python script with the JSON data
$result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));

// Decode the result
$resultData = json_decode($result, true);

// This will contain: array('status' => 'Yes!')
var_dump($resultData);

Python:

import sys, json

# Load the data that PHP sent us
try:
    data = json.loads(sys.argv[1])
except:
    print "ERROR"
    sys.exit(1)

# Generate some data to send to PHP
result = {'status': 'Yes!'}

# Send it to stdout (to PHP)
print json.dumps(result)
Answered By: Tom van der Woerdt

You are looking for “interprocess communication” (IPC) – you could use something like XML-RPC, which basically lets you call a function in a remote process, and handles the translation of all the argument data-types between languages (so you could call a PHP function from Python, or vice versa – as long as the arguments are of a supported type)

Python has a builtin XML-RPC server and a client

The phpxmlrpc library has both a client and server

There are examples for both, Python server and client, and a PHP client and server

Answered By: dbr

Just had the same problem and wanted to share my solution. (follows closely what Amadan suggests)

python piece

import subprocess

output = subprocess.check_output(["php", path-to-my-php-script, input1])

you could also do: blah = input1 instead of just submitting an unnamed arg… and then use the $_GET[‘blah’].

php piece

$blah = $argv[1];



if( isset($blah)){

    // do stuff with $blah

}else{
    throw new Exception('No blah.');
}
Answered By: fchaubard

For me the escapeshellarg(json_encode($data)) is giving not exactly a json-formatted string, but something like { name : Carl , age : 23 }.
So in python i need to .replace(' ', '"') the whitespaces to get some real json and be able to cast the json.loads(sys.argv[1]) on it.

The problem is, when someone enters a name with already whitespaces in it like "Ca rl".

Answered By: Morizzla
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.