An open API service indexing awesome lists of open source software.

https://github.com/omaralalwi/php-py

Interact with python in PHP applications
https://github.com/omaralalwi/php-py

deepseek-r1 php php-ai php-python-scripts pyp-excute-python python python-in-php python-lambda

Last synced: 4 months ago
JSON representation

Interact with python in PHP applications

Awesome Lists containing this project

README

          

# PhpPy - PHP Python ๐Ÿš€๐Ÿ
Seamlessly enabling secure and efficient execution of Python scripts within PHP applications without spread multiple applications and or setup API.

### ๐Ÿ›  [Laravel py package](https://github.com/omaralalwi/laravel-py)

---

## ๐Ÿ“Œ Table of Contents

- [๐Ÿ“– Overview](#-overview)
- [๐Ÿ”ง Requirements](#requirements)
- [๐Ÿš€ Installation](#installation-)
- [๐Ÿš€ Quick Start](#-quick-start)
- [โœจ Features](#-features)
- [๐Ÿ›  Framework Integration](#-framework-integration)
- [๐Ÿ“‹ Changelog](#-changelog)
- [๐Ÿงช Testing](#-testing)
- [๐Ÿ”’ Security](#-security)
- [๐Ÿค Contributors](#-contributors)
- [๐Ÿ“„ License](#-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](https://www.python.org/) must be installed in server .

---

## Installation ๐Ÿ› ๏ธ

You can install the package via Composer:

```bash
composer require omaralalwi/php-py
```

---

## ๐Ÿš€ Quick Start

1. ๐Ÿ“‚ Create a folder for scripts, e.g., `phpPyScripts` in your project root directory.
2. ๐Ÿ“ Create a Python script file (`.py` extension) and write Python code. [See this script examples](https://github.com/omaralalwi/php-py/tree/master/example-scripts).
3. ๐Ÿ”ง make script file executable, `chmod +x script_file_path` .

### โšก Easy Usage

```php
'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
'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
'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_open` as an alternative to `shell_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](CHANGELOG.md) ๐Ÿ“œ

---

## ๐Ÿงช Testing

```bash
./vendor/bin/pest
```

---

## ๐Ÿ”’ Security

**Report Vulnerabilities**: Contact [omaralwi2010@gmail.com](mailto:omaralwi2010@gmail.com) ๐Ÿ“ฉ

---

## ๐Ÿค Contributors

A huge thank you to these amazing people who have contributed to this project! ๐ŸŽ‰๐Ÿ’–




Omar AlAlwi


Omar AlAlwi



๐Ÿ† Creator

**Want to contribute?** Check out the [contributing guidelines](./CONTRIBUTING.md) and submit a pull request! ๐Ÿš€

---

## ๐Ÿ“„ License

This package is open-source software licensed under the [MIT License](LICENSE.md). ๐Ÿ“œ