executing Python script in PHP and exchanging data between the two

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 ?


Solution 1:

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)

Solution 2:

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

Solution 3:

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.');
}