Viewed   58 times

Say you have a class like this:

class MyClass:
    def __init__(self, var1):
        self.var = var1
    ....

This class, in python, works only when you assign a value:

x = MyClass("Hi")

So basically, my question is whether I can send a variable from php to execute a python class, and return its output (it's string) and continue to execute my php code?

Any suggestions?

SOLUTION

in php:

$var = "something";
$result = exec("python fileName.py .$var")

in python:

import sys

sys.argv[0] # this is the file name
sys.argv[1] # this is the variable passed from php

 Answers

4

First of all, create a file containing the python-script you want to execute, including (or loading) the class and x = MyClass("Hi")

Now, use the following line to get the result:

$result = exec('python yourscript.py');
Saturday, October 29, 2022
 
2
  • download the PhantomJS binary, upload it somewhere and make it executable (chmod +x)
  • if you are going to make screenshots, setup fontconfig (this is pretty specific to my config but the goal is to make sure to have at least some fonts on your system)
  • run the following in PHP:
    $response = exec('/path/to/phantomjs myscript.js');
Saturday, December 24, 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
 
2

wkhtmltopdf has bindings, one of them is for PHP. You could give those a shot.

Thursday, September 29, 2022
 
arbr
 
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
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 :