Home  >  Article  >  Backend Development  >  How to do Python integration in PHP?

How to do Python integration in PHP?

WBOY
WBOYOriginal
2023-05-20 21:21:092294browse

PHP is a popular server-side scripting language, while Python is a widely used high-level programming language used in many fields such as web development, data science, artificial intelligence, and more. Integrating PHP with Python can bring more possibilities and advantages to web development. This article will introduce how to do Python integration in PHP.

1. Python installation and configuration

Before integrating PHP and Python, we need to install Python on the server. You can visit the Python official website to download the Python installation package for installation. After the installation is complete, you need to configure the environment variables so that you can call Python commands anywhere.

After installing Python, we need to install the Python plug-ins numpy and matplotlib. These two plug-ins are important plug-ins for data processing, visualization and scientific computing in Python, and are usually used in fields such as machine learning and data analysis. Enter the following command in the terminal to install:

$ pip install numpy
$ pip install matplotlib

2. PHP calls Python program

You can use the exec() function to execute system commands in PHP, so you can implement PHP by calling Python's execution command Integration with Python. The specific steps are as follows:

  1. Use the exec() function in the Php file to call the Python command:
<?php
    $command = escapeshellcmd('/usr/bin/python script.py');
    $output = shell_exec($command);
    echo $output;
?>

In this example, we executed a file called "script.py "Python script. This Python script can be any Python code you want to execute. Among them, the escapeshellcmd() function can escape the script file path to prevent arbitrary command execution vulnerabilities.

  1. Processing data in Python script:
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 4*np.pi, 0.1)
y = np.sin(x)

plt.plot(x, y)
plt.show()

The Python script here uses numpy and matplotlib libraries to process data and draw charts. When PHP calls this script, Python will calculate the value of the sine function y and generate a chart, and then pass the result of the chart back to PHP for display on the web page.

3. PHP and Python data interaction

When using exec() in PHP to call a Python script, we can use PHP’s output caching mechanism to capture the output of the Python script, and then use the output as String returned. PHP's output caching mechanism can be implemented using the ob_start() and ob_get_clean() functions.

After returning the output as a string, we can use PHP's built-in JSON library to convert the data output by the Python script into JSON format, and then pass the JSON data back to the web page. In this way, we can exchange and share data between PHP and Python.

4. PHP and Python process management

When PHP calls a Python script, we need to ensure that the Python process is started and closed correctly. You can use PHP's pcntl_fork() and pcntl_wait() functions to manage Python processes. You can also use third-party process management libraries to manage Python processes, such as Supervisor and PM2.

Supervisor is a Python tool for managing processes. It can be used to start, stop, restart and manage multiple Python processes. PM2 is a tool for managing Node.js processes and also supports the management of Python processes. These process management tools can help us better manage the life cycle of the Python process and reduce the risk of program errors.

Summary

PHP and Python integration can bring more possibilities and advantages to web development. This article introduces how to use the exec() function in PHP to call Python scripts and exchange data through the output caching mechanism and JSON data format. At the same time, we also introduced how to use process management tools such as Supervisor and PM2 to manage the life cycle of Python processes. Through these means, we can better realize the integration of PHP and Python, thereby expanding the application scenarios of web development.

The above is the detailed content of How to do Python integration in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn