Using python code in php

PHP — How to run Python script with PHP code

The shell_exec() function allows you to run a command from the shell (or terminal) and get the output as a string.

Since the function runs a command from the shell, you need to have Python installed and accessible from your computer.

PHP can’t run Python scripts directly. It just passes a command to the shell to run a Python script.

For example, suppose you have a hello.py script with the following code:

To run the script above, you need to write the following code to your PHP file:
 The Python script that you want to run needs to be passed as an argument to the shell_exec() function.

The echo construct will print the output of the script execution.

If you see the shell responds with python: command not found , then that means the python program can’t be found from the shell.

You need to make sure that the python program can be found by running the which command as follows:

 You may also have a Python interpreter saved as python3 , which is how Python is installed in the latest macOS version.

In this case, you need to run the script using python3 in the shell_exec() function:

In a UNIX environment, you can also specify the Python interpreter in the .py file as the shebang line.

Write the interpreter you want to use for the script as follows:

 With the shebang line defined, you can remove the python runner from the shell_exec() function:
Now run the PHP script. You should see the same output as when you add the runner to the shell_exec() function.

Sometimes you may see the shell responds with permission denied as follows:

This means that the PHP runner doesn’t have the execute permission for the Python script that you want to run.

To fix this, you need to add the execute permission to the Python script with chmod like this:

When you run the above command from the shell, the execute permission ( x ) will be added to the file.

And that’s how you can run Python script with PHP. Nice! 👍

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

How to Run a Python Script in PHP

PHP (which is a recursive acronym for PHP Hypertext Preprocessor) is one of the most widely used web development technologies in the world. PHP code used for developing websites and web applications can be embedded directly along with the rest of the HTML markup. This, along with a rich amount of libraries to perform system tasks, or to integrate with other application, makes PHP the go-to language for the Internet.

While there are plenty of libraries (or modules) in PHP, there can be cases when a module for a certain task is either missing or is not properly implemented; while the same module is available in Python.

Apart from this, for certain scripting tasks the user might find Python more suitable, while he finds PHP suitable for the rest of his codebase.

In this article, we will take a look at how a Python script can be run from a PHP interpreter in a Linux terminal.

Calling a Python Script in PHP

Let us consider the following PHP code (test.php).

Note: The ‘\n’ at the end of the string is a newline character, which will move the cursor to the new line for the next commands on the terminal.

Let’s now call a simple Python script ‘ test.py ’, which simply prints ‘Hello World’.

$ cat test.py print(“Hello World”) 

To call this script from PHP, use the ‘exec’ function.

We can call the script with the command line program ‘php’, which is nothing but the PHP interpreter.

$ cat test.py $ cat test.php $ php test.php

Run Python Script Using PHP

As we can see, the output of Python script test.py was displayed.

Next, let’s consider the following Python script file, ‘test2.py’, which takes one argument, a String, and prints it.

$ cat test2.py import sys print(sys.argv[1])

Now let’s call this script from PHP, passing the argument.

Execute Python Script Using PHP

As we can see, the Python Script got executed and printed the parameter value ‘Example’.

Conclusion

In this article, we saw how to call a Python script from within our PHP code. This can be used on more complex Python scripts, the output of which will be stored in the PHP variable as shown in our examples above.

This can especially be helpful when some complex Python libraries, eg. the machine learning and data science libraries, are to be used. If you have any questions or feedback, make sure you leave a comment below!

Источник

PHP — How to run Python script with PHP code

The shell_exec() function allows you to run a command from the shell (or terminal) and get the output as a string.

Since the function runs a command from the shell, you need to have Python installed and accessible from your computer.

PHP can’t run Python scripts directly. It just passes a command to the shell to run a Python script.

For example, suppose you have a hello.py script with the following code:

To run the script above, you need to write the following code to your PHP file:
 The Python script that you want to run needs to be passed as an argument to the shell_exec() function.

The echo construct will print the output of the script execution.

If you see the shell responds with python: command not found , then that means the python program can’t be found from the shell.

You need to make sure that the python program can be found by running the which command as follows:

 You may also have a Python interpreter saved as python3 , which is how Python is installed in the latest macOS version.

In this case, you need to run the script using python3 in the shell_exec() function:

In a UNIX environment, you can also specify the Python interpreter in the .py file as the shebang line.

Write the interpreter you want to use for the script as follows:

 With the shebang line defined, you can remove the python runner from the shell_exec() function:
Now run the PHP script. You should see the same output as when you add the runner to the shell_exec() function.

Sometimes you may see the shell responds with permission denied as follows:

This means that the PHP runner doesn’t have the execute permission for the Python script that you want to run.

To fix this, you need to add the execute permission to the Python script with chmod like this:

When you run the above command from the shell, the execute permission ( x ) will be added to the file.

And that’s how you can run Python script with PHP. Nice! 👍

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Search

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

PHPython: An extension to eval python3 codes in PHP

bullsoft/PHPython

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

An extension to eval python codes in PHP

  • pybind11 V2.1.1
  • PHP-CPP-LEGACY V1.5.7 / PHP-CPP V2.x
  • PHP 5 / PHP 7
  • Python 3
  • C++ 11

Variables defined in Python

$code =  a = [1, 2, 3] EOD; $python = new Python(); $python->eval($code); var_export($python->extract("a"));

. you can use extract method in php to get that python variable, codes above output:

$a = ["a" => "b", "c" => "d"]; $code =  print(a) EOD; $python = new Python(); $python->assign("a", $a); $python->eval($code);

. you can use assign method in php to let python know that php variable, codes above output:

Functions defined in Python

$a = ["a" => "b", "c" => "d"]; $code =  def dofunc(arg1, arg2): print("Python Output") print(arg1) print(arg2) return > after = "abcd" EOD; $python = new Python(); $python->eval($code); $python->assign("tmp", $a); var_dump("PHP Here. ", $python->dofunc($a, "py::tmp"));

. you can call functions defined in Python as-is-a $python->method() , codes above output:

  string(11) "PHP Here. " array(1) < 'Python' =>array(2) < 'a' =>array(2) < 'a' =>string(1) "b" 'c' => string(1) "d" > 'b' => array(2) < 'a' =>string(1) "b" 'c' => string(1) "d" > > > 

Here, We can also use these ways to call functions defined in Python:

var_dump("PHP Here. ", $python->call("dofunc", [$a, "py::tmp"])); var_dump("PHP Here. ", $python->call("dofunc(after, tmp)"));
Python Output // $a in php // tmp in python which assigned by php string(11) "PHP Here. " array(1) < 'Python' =>array(2) < 'a' =>array(2) < 'a' =>string(1) "b" 'c' => string(1) "d" > 'b' => array(2) < 'a' =>string(1) "b" 'c' => string(1) "d" > > > Python Output abcd // after in python // tmp in python which assigned by php string(11) "PHP Here. " array(1) < 'Python' =>array(2) < 'a' =>string(4) "abcd" 'b' => array(2) < 'a' =>string(1) "b" 'c' => string(1) "d" > > > 
$code =  tmp = print("Python begin") print(dofunc(tmp)) print("Python end") EOD; $python = new Python(); $python->def("dofunc", function($param) < echo __function__ . " in PHP: Get params from Python :" . PHP_EOL; echo var_export($param, true) . PHP_EOL; return [ "php" => $param ]; >); $python->eval($code);

. you can call php funciton in python as-is-a real python function, codes above output:

Python begin in PHP: Get params from Python : array ( 0 => array ( 'a' => 'b', 'c' => 'd', ), ) ]> Python end 

if you like, you can also call that function using the php way, like this:

var_dump($python->dofunc("py::tmp", "phpString"));
 in PHP: Get params from Python : array ( 0 => array ( 'a' => 'b', 'c' => 'd', ), 1 => 'phpString', ) array(1) < 'php' =>array(2) < [0] =>array(2) < 'a' =>string(1) "b" 'c' => string(1) "d" > [1] => string(9) "phpString" > > 

PHP Call Python Function

Python Call PHP Function

About

PHPython: An extension to eval python3 codes in PHP

Источник

Читайте также:  Ресурсы изучение html css
Оцените статью