Seamlessly enabling secure and efficient execution of Python scripts within PHP applications without spread multiple applications and or setup API.
- π Overview
- π§ Requirements
- π Installation
- π Quick Start
- β¨ Features
- π Framework Integration
- π Changelog
- π§ͺ Testing
- π Security
- π€ Contributors
- π License
The PhpPy
package provides seamless integration between PHP and Python without API, enabling secure and efficient execution of Python scripts within PHP applications. It ensures structured script execution while managing configurations, arguments, environment variables, and error handling.
- PHP 8.1+
- python3 must be installed in server .
You can install the package via Composer:
composer require omaralalwi/php-py
- π Create a folder for scripts, e.g.,
phpPyScripts
in your project root directory. - π Create a Python script file (
.py
extension) and write Python code. See this script examples. - π§ make script file executable,
chmod +x script_file_path
.
<?php
require_once 'vendor/autoload.php';
use Omaralalwi\PhpPy\PhpPy;
use Omaralalwi\PhpPy\Managers\ConfigManager;
$configManager = new ConfigManager([
'scripts_directory' => 'phpPyScripts',
'python_executable' => '/usr/bin/python3',
'max_timeout' => 120,
]);
try {
$result = PhpPy::build()
->setConfig($configManager)
->loadScript('sum_calculator.py')
->withArguments([10, 20, 30])
->run();
print_r($result); // 60.0
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
<?php
require_once 'vendor/autoload.php';
use Omaralalwi\PhpPy\PhpPy;
use Omaralalwi\PhpPy\Managers\ConfigManager;
$configManager = new ConfigManager([
'scripts_directory' => 'phpPyScripts',
'python_executable' => '/usr/bin/python3',
'max_timeout' => 120,
]);
try {
$result = PhpPy::build()
->setConfig($configManager)
->loadScript('advanced_example.py')
->withArguments([10, 20, 30])
->withEnvironment(['FIRST_ENV_VAR' => 'some value', 'SECOND_ENV_VAR' => 'some value'])
->timeout(30)
->asJson()
->run();
print_r(json_decode($result));
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
Example: Running DeepSeek AI on your server while handling API requests using PHP.
<?php
require_once 'vendor/autoload.php';
use Omaralalwi\PhpPy\PhpPy;
use Omaralalwi\PhpPy\Managers\ConfigManager;
$configManager = new ConfigManager([
'scripts_directory' => 'deepSeekScripts',
'python_executable' => '/usr/bin/python3',
'max_timeout' => 120,
]);
header('Content-Type: application/json');
$valid_tokens = ['USER1' => 'abcd1234', 'USER2' => 'efgh5678'];
$token = $_POST['token'] ?? '';
if (!isset($valid_tokens[$token])) {
echo json_encode(['error' => 'Invalid token']);
exit;
}
$prompt = $_POST['prompt'] ?? '';
if (!empty($prompt)) {
$clean_prompt = escapeshellarg($prompt);
$response = PhpPy::build()
->setConfig($configManager)
->loadScript('model_worker.py')
->withArguments($clean_prompt)
->timeout(30)
->asJson()
->run();
echo json_encode(['response' => trim($response)]);
} else {
echo json_encode(['error' => 'No prompt provided']);
}
- Path Validation β Ensures scripts are within allowed directories.
- Argument & Environment Validation π Restricts unauthorized input.
- Timeout Control β³ Prevents long-running scripts.
- black list for these vars
PATH,PYTHONPATH,LD_LIBRARY_PATH,LD_PRELOAD,PYTHONHOME
, can not passed . - Uses
proc_open
as an alternative toshell_exec
.
- Centralized settings via
ConfigManager
. - Customizable execution parameters.
- Supports JSON parsing.
- Captures and reports script errors.
- Detailed exception handling for debugging.
- Standardized error reporting.
- Modular execution through
CommandExecutor
. - Customizable for advanced use cases.
β Never pass user-controlled input directly script, just pass scripts that you will need as a administrator (Just from Your side) .
See detailed release notes in CHANGELOG.md π
./vendor/bin/pest
Report Vulnerabilities: Contact [email protected] π©
A huge thank you to these amazing people who have contributed to this project! ππ
Omar AlAlwi π Creator |
Want to contribute? Check out the contributing guidelines and submit a pull request! π
This package is open-source software licensed under the MIT License. π