skip to Main Content

I have a web application that uses jQuery (via ajax) to PHP and from there to a MySQL database.
I need to do some ML using Python and reading from the database. My problem is that I am unable to figure out how to communicate from PHP to Python and back to PHP?

For more context, this will be a food recommendation system so on the website, if the user wants recommendations they can select filters (e.g. price range). Now at the moment, this will send a request to PHP and I am trying to pass data to Python run the recommender and get back to PHP with the returned data. Or is there a better way to do this? Like communicate from jquery to Python straight away. (I have Python running the recommender by itself communicating with the database, it just needs the filters that are in jquery)

2

Answers


  1. You could use something like WebSockets to communicate from your front end to the Python backend. WebSockets are basically elevated HTTP sessions which allow for bidirectional transmission.

    Mozilla docs have great information on WebSocket API that’s built into modern browsers.
    Here

    This would be if you wanted to communicate straight from the front-end to python. If you wanted to do it from the backend (php), you could establish a socket connection from php to your python program. (TCP). But this might a bit overkill.

    Login or Signup to reply.
  2. Maybe as a simpler alternative, you could create a small Flask web application with an endpoint that can be called by your PHP application (using an HTTP client).

    The Flask web app can then make use of all regular Python functionality such as the ML libraries you require and return the result to your PHP application.

    Or call the service directly through an ajax request.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search