Viewed   70 times

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 ?

 Answers

3

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)
Sunday, November 20, 2022
5

Using PDO prepared statements:

$placeholders = str_repeat('?, ', count($array)-1) . '?';
$stmt = $pdo->prepare("SELECT * FROM table WHERE field IN ($placeholders)");
$stmt->execute($array);

$placeholders will contain a sequence of ?, ?, ? placeholders, with the same number of ? as the size of the array. Then when you execute the statement, the array values are bound to the placeholders.

Saturday, August 27, 2022
 
xorlev
 
4

No. When a process is killed, the operating system releases all operating system resources (memory, sockets, file handles, …) previously acquired by that process.

Sunday, September 18, 2022
 
vsb
 
vsb
1

Update -

Now that I am aware of PHP, the mistake lies in using the single-quotes '. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted " strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,

$output = passthru("python wordgame2.py $start_word $end_word");

would work, but the following won't -

$output = passthru('python wordgame2.py $start_word $end_word');

Original answer -

I think the mistake lies in

$output = passthru("python wordgame2.py $start_word $end_word");

Try this

$output = passthru("python wordgame2.py ".$start_word." ".$end_word);
Tuesday, November 15, 2022
 
3

I find it odd that the filePath of C:\wamp\.... would work on a Linux machine. Anyway, have you tried NOT escaping the slashes?

The issue can be related to several things, including OS specific methods of implementing the different program execution functions. I recommend just going through the different options, I always put the command into a single variable and then print out the variable so that I can see what I am about to run, if you saw two slashes, then you might know that there is no need to try to escape the slashes, however without printing it to the screen you wouldn't know.

Try going through the other process execution functions to see what works for you. http://www.php.net/manual/en/ref.exec.php

The one that I have found works MOST like running it at the command line, is the backtick operator, and then next is system

try something like this.

$pyscript = 'C:\wamp\www\testing\scripts\imageHandle.py';
$python = 'C:\Python27\python.exe';
$filePath = 'C:\wamp\www\testing\uploads\thumbs\10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd`


$pyscript = 'C:wampwwwtestingscriptsimageHandle.py';
$python = 'C:Python27python.exe';
$filePath = 'C:wampwwwtestinguploadsthumbs10-05-2012-523.jpeg'

$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd` 

EDIT: AHHH! I just figured it out, you are surrounding your command with single ticks '' which means the variable inside are not being replaced. Use double quotes instead. That WILL solve the problem, but I still recommend echoing the command out before you run it so that you would see the command you are running and you would be able to identify an issue like this.

Tuesday, August 23, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :