PHP code example of omaralalwi / php-py
1. Go to this page and download the library: Download omaralalwi/php-py library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
omaralalwi / php-py example snippets
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();
}
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();
}
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']);
}
bash
composer