omaralalwi/php-py
Composer 安装命令:
composer require omaralalwi/php-py
包简介
play with python in PHP applications
关键字:
README 文档
README
Seamlessly enabling secure and efficient execution of Python scripts within PHP applications without spread multiple applications and or setup API.
🛠 Laravel py package
📌 Table of Contents
- 📖 Overview
- 🔧 Requirements
- 🚀 Installation
- 🚀 Quick Start
- ✨ Features
- 🛠 Framework Integration
- 📋 Changelog
- 🧪 Testing
- 🔒 Security
- 🤝 Contributors
- 📄 License
📖 Overview
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.
Requirements
- PHP 8.1+
- python3 must be installed in server .
Installation 🛠️
You can install the package via Composer:
composer require omaralalwi/php-py
🚀 Quick Start
- 📂 Create a folder for scripts, e.g.,
phpPyScriptsin your project root directory. - 📝 Create a Python script file (
.pyextension) and write Python code. See this script examples. - 🔧 make script file executable,
chmod +x script_file_path.
⚡ Easy Usage
<?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(); }
🔥 Advanced Usage
<?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(); }
🌍 Real-World Example
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']); }
✨ Features
🔐 Secure Execution
- 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_openas an alternative toshell_exec.
🔧 Flexible Configuration
- Centralized settings via
ConfigManager. - Customizable execution parameters.
📤 Output Handling
- Supports JSON parsing.
- Captures and reports script errors.
🚨 Error Management
- Detailed exception handling for debugging.
- Standardized error reporting.
🔌 Extensibility
- Modular execution through
CommandExecutor. - Customizable for advanced use cases.
Important Critical Note:
❌ Never pass user-controlled input directly script, just pass scripts that you will need as a administrator (Just from Your side) .
📋 Changelog
See detailed release notes in CHANGELOG.md 📜
🧪 Testing
./vendor/bin/pest
🔒 Security
Report Vulnerabilities: Contact omaralwi2010@gmail.com 📩
🤝 Contributors
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! 🚀
📄 License
This package is open-source software licensed under the MIT License. 📜
omaralalwi/php-py 适用场景与选型建议
omaralalwi/php-py 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 266 次下载、GitHub Stars 达 19, 最近一次更新时间为 2025 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「python」 「python-php」 「omaralalwi」 「DeepSeek-R1」 「php-py」 「php-api-server」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 omaralalwi/php-py 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 omaralalwi/php-py 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 omaralalwi/php-py 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Non-standard PHP library (NSPL) - functional primitives toolbox and more
PHP implementation of python's struct module.
Collection are Dictionaries, Maps, Lists and Iterators for php
Laravel Taxify provides a set of helper functions and classes to simplify tax (VAT) calculations within Laravel applications. that allow developers to easily integrate tax calculation functionalities into their projects. it's offers a straightforward and efficient solution Designed to streamline the
clean logs and debug files (clockwork , laravel telescope and more)
sample php traits to add ability to use builder design patterns with easy in PHP applications
统计信息
- 总下载量: 266
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 31
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-07