How to create Python session from PHP
I want to create a Python session and call Python API from PHP. As my Python script needs to load prebuild model and load libraries on session startup, running a new session (running a script) every time running a Python command is not a good idea. So, I want to create a Python session in the background with predefined commands, and later I can input commands at any time and get the output of Python.
For example, when I was using R, there is a package called 'reticulate' which call Python API and it keeps the Python session. So, I built Web-app with (R) shiny-server and a single session Python.
I googled all the possible keywords, but all results are 'how to run python script from php'. I am pretty sure that if I ask this question on StackOverflow, it will be marked as duplicated with those 'how to run python script from php'. So, I come here to get help. 😁 Any references or answers are appreciated. Thanks in advance.
3 comments · 60% upvoted · other discussions
Not sure about your particular Python App, but on the client side a session is usually just a session cookie with a token that get matched on the server. So what you would do in PHP is :
For example if you use Guzzle to make your request you can see the documentation about cookies here : https://docs.guzzlephp.org/en/stable/quickstart.html#cookies