15

I have a VPS Linux webserver with PHP installed. I want to code some search engine or data mining stuff in one application which I am thinking of building in python.

I want to know if it is possible to use python and php together like calling functions of python in php and vice versa.

As it is my VPS server, I can install anything on that. Has anyone tried using python in php? Are there any performance issues in real time??

TheSoldier
  • 484
  • 1
  • 5
  • 25
  • possible duplicate of [Calling Python in PHP](http://stackoverflow.com/questions/166944/calling-python-in-php) – S.Lott Jun 21 '10 at 12:14
  • Instead try Flask! It is a micro web development framework using Python. So basically you code the thing using Python. So no worries about hacking your way through things like using PHP with Python. – Rahul Soshte Jun 28 '18 at 15:40

5 Answers5

12

You can execute python scripts using the exec() function in your php script.

Also this seems to provide an answer or two to your question.

Calling Python in PHP

Community
  • 1
  • 1
Bella
  • 3,209
  • 23
  • 26
4

You could have a look at PiP

To that end, I've [site author] written a Python extension for PHP. In short, this extensions allows the Python interpretter to be embedded inside of PHP (think of PHP as the parent language with Python as its child). This allows native Python objects to be instantiated and manipulated from within PHP. There is also initial support for accessing PHP functions and data from within the embedded Python environment.

However, I cannot comment on its reliability. Might need to test it for yourself.

Russell Dias
  • 70,980
  • 5
  • 54
  • 71
1

You're trying to do something like

def helloWorld():
    print 'Hello, World'

<?php helloWorld(); ?>

I'd say that you most certainly can't.

Edit: Have a look at php's shell_exec though.

cypher
  • 6,822
  • 4
  • 31
  • 48
1

I think that should be

<?php 

$try = exec("/var/htdocs/folder/test.py")

?>

Try and see if it works

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
-1

Better you have to do, use api, different application like one application on python server having python application and one server having php application with api. Like you store or delete data by using api in android or ios.

Ronny Dsouza
  • 358
  • 2
  • 12
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 23 '21 at 08:38