
How to Integrate Python with PHP: Practical Guide for Modern Web Projects
28 Jul 2025Discover how to connect Python with PHP, run scripts, exchange data, and boost your web projects with real-life examples and clear steps.
Ever needed a Python library but your site lives in PHP? You’re not alone. Mixing the two can feel scary, but it’s really just about passing text back and forth. Below you’ll find the fastest ways to call Python from PHP, plus a few tricks for larger projects.
The quickest method is PHP’s exec()
or shell_exec()
. Write a tiny Python script, point PHP at it, and capture the output.
$cmd = 'python3 /path/to/script.py "' . escapeshellarg($input) . '"';
$result = shell_exec($cmd);
echo $result;
Two things to watch:
escapeshellarg()
on user data. One stray quote and you open a security hole.python3
and read the script file.This works great for one‑off calculations, CSV generation, or calling a machine‑learning model that lives in a separate file.
When you need tighter coupling, exec starts to feel clunky. Here are three alternatives that keep things clean.
1. PHP‑Python bridge (php‑python) – a PECL extension that lets you import Python modules directly into PHP. After installing the extension, you can do:
$py = new Python();
$py->import('math');
$sqrt = $py->call('math.sqrt', 16);
echo $sqrt; // 4
The bridge handles data conversion for you, so numbers stay numbers and strings stay strings. It does require server‑level installation, but performance beats spawning a new process each time.
2. HTTP API – spin up a tiny Flask or FastAPI service that listens on localhost. Your PHP code sends a POST request with JSON, the Python side does the work, and returns JSON.
$payload = ['text' => $text];
$ch = curl_init('http://127.0.0.1:5000/analyze');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
This approach isolates the two runtimes, making debugging easier and letting you scale the Python service independently.
3. Socket communication – for low‑latency needs, open a TCP socket from PHP and talk to a long‑running Python daemon. The daemon reads a line, processes it, and writes back.
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, '127.0.0.1', 9000);
socket_write($sock, $message . "\n");
$response = socket_read($sock, 2048);
socket_close($sock);
Because the Python process stays alive, you avoid the start‑up cost of launching a script for each request.
Whichever method you choose, keep these best practices in mind:
set_time_limit()
in PHP and timeout
flags in Python to avoid runaway processes.stderr
from exec or return HTTP status codes from your API so you can spot problems fast.Mixing PHP and Python doesn’t have to be a nightmare. Start with shell_exec()
for quick experiments, then graduate to a bridge or an API as your needs grow. You’ll get the best of both worlds – PHP’s web friendliness and Python’s powerful libraries – without rewriting your whole codebase.
Discover how to connect Python with PHP, run scripts, exchange data, and boost your web projects with real-life examples and clear steps.