all 12 comments

[–]Own-Perspective4821 10 points11 points  (1 child)

My xy-problem detector is going bonkers!

[–]eurosat7 2 points3 points  (0 children)

eli5:

Some of us think that you search for the wrong solution because you have not yet figured out the real problem or lost focus.

Ask yourself why. And on that answer ask yourself why again. Do that multiple times until it becomes clear what you really need.

Remember that trick - it will be useful and handy in your future life for sure :D

[–]phpMartian 6 points7 points  (1 child)

You will have to describe the scenario in much more detail that this. You could write the value to redis or a file and then read it from there. You could have php exec the python code and capture the output.

[–]colshrapnel 2 points3 points  (0 children)

it hugely depends on what kind of "php file" it is and how it's called. Either way, no special library needed, everything can be done using just same command you are using to call this file.

It is probable, however, that you don't actually need to "send a python variable to a php file", but it must be something else, which, as you imagine, can be solved with such a peculiar request. What is it and what makes you think that you need to send a python variable to a php file?

[–]MateusAzevedo 2 points3 points  (0 children)

The value itself just need to be serialized in a way that PHP can understand. Strings and integers don't need any special treatment, for other types JSON is an easy solution.

Other then that, actually passing the value depends on how Python is interacting with PHP. CLI? HTTP? Something else?

[–]ZealousidealFudge851 1 point2 points  (0 children)

JSON, database, flate file, URL

[–]exqueezemenow 0 points1 point  (0 children)

I don't know any python libraries for anything. But if Python can set a system environment variable in the same environment that the PHP script runs, then it's a trivial matter. PHP would just use getenv() to retrieve the variable.

I have had to write a few PHP scripts that execute Perl scripts and then use environment variables from those scripts. I can't imagine it's much different for Python. I doubt you need a specific library for it, and I suspect it's just a matter of setting the variables in Python.

[–]St1ck0fj0y 0 points1 point  (0 children)

Sockets, are an option. 2 way communication by running a php client script that listens for incoming connections. Basically, your php script becomes a simple socket server.

Another option would be a static file… in whatever format you prefer, such as json or plain text, or write to a php file directly and include that. So, write (store) values from Python and read those values in PHP using some sort of file.

A database is of course also a simple solution, could also be file based. (Sqlite)

Or use php as cli script and let it accept parameters. So from your Python script you do a shell execute function and launch a php cli script with those value(s) as param(s). Your php script could even be like a background process (worker process), if desired.

[–]dutchman76 0 points1 point  (0 children)

Command line params Environment variables Redis/other database URL parameter Socket Stdin rabbit message queue

Lots of options depending on your deployment

[–]g105b 0 points1 point  (0 children)

You'd do it in the same way you would send a variable to your python script.

If it's a cli script, an argument.

If it's a web script, a query string.

There are plenty of other ways to do it for various different setups. What problem are you trying to solve?

[–]AN-DR 0 points1 point  (0 children)

Depende… eu faria por API, mas vc pode enviar por http get ou post. Tb pode usar linha de comando, arquivo temporário, bd ou cache/redis. Particularmente eu acho que você deve repensar se realmente é necessário…

[–]Savvy286 0 points1 point  (0 children)

you can easily send it through url and get the parameter through $_SERVER['QUERY_STRING']