使用者:游士賢:修訂版本之間的差異

出自福留子孫
跳轉到: 導覽搜尋
(移除所有頁面內容)
第 1 行: 第 1 行:
 +
==php呼叫python顯示於網頁==
 +
===範例一、文字顯示===
 +
[http://jendo.org/~游士賢/chatgcp/test.php 範例程式]
  
 +
'''test.php'''
 +
<pre>
 +
 +
<!DOCTYPE html>
 +
<html>
 +
<head>
 +
    <meta charset="UTF-8">
 +
    <title>PHP呼叫Python範例</title>
 +
</head>
 +
<body>
 +
<?php
 +
    $output = shell_exec('python word.py');    // 用 shell_exec 函式呼叫 Python 程式並取得輸出
 +
    echo "<pre>$output</ pre>";                // 將輸出作為純文本輸出到 HTML 頁面上
 +
?>
 +
</body>
 +
</html>
 +
 +
</pre>
 +
 +
'''word.py'''
 +
<pre>
 +
print("Hello, world!")
 +
</pre>
 +
 +
===範例二、兩數相加算術===
 +
[http://jendo.org/~游士賢/chatgcp/index.php 範例程式]
 +
 +
'''index.php'''
 +
<pre>
 +
<!DOCTYPE html>
 +
<html>
 +
<head>
 +
<title>兩數相加算術範例</title>
 +
</head>
 +
<body>
 +
<h2>兩數相加算術</h2>
 +
<form method="POST">
 +
<label>請輸入第一個數字:</label>
 +
<input type="text" name="num1">
 +
<br><br>
 +
<label>請輸入第二個數字:</label>
 +
<input type="text" name="num2">
 +
<br><br>
 +
<input type="submit" name="submit" value="計算">
 +
</form>
 +
<?php
 +
if(isset($_POST['submit'])){
 +
$num1 = $_POST['num1'];
 +
$num2 = $_POST['num2'];
 +
 +
$python_path = "/usr/bin/python"; // 設定 Python 可執行文件的路徑
 +
$python_script = "calculate.py"; // 要執行的 Python 腳本路徑
 +
 +
// 執行 Python 腳本,並傳遞數字參數
 +
$command = escapeshellcmd("$python_path $python_script $num1 $num2");
 +
$output = shell_exec($command);
 +
 +
echo "<p>答案:$output</p>"; // 在網頁上顯示 Python 腳本的輸出
 +
}
 +
?>
 +
</body>
 +
</html>
 +
</pre>
 +
 +
'''calculate.py'''
 +
<pre>
 +
 +
import sys
 +
 +
# 獲取從 PHP 傳遞的兩個數字參數
 +
num1 = float(sys.argv[1])
 +
num2 = float(sys.argv[2])
 +
 +
# 執行算術操作
 +
result = num1 + num2
 +
 +
# 將結果返回給 PHP
 +
print(result)
 +
</pre>

2023年3月14日 (二) 14:05的修訂版本

php呼叫python顯示於網頁

範例一、文字顯示

範例程式

test.php


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP呼叫Python範例</title>
</head>
<body>
<?php
    $output = shell_exec('python word.py');    // 用 shell_exec 函式呼叫 Python 程式並取得輸出
    echo "<pre>$output</ pre>";                // 將輸出作為純文本輸出到 HTML 頁面上
?>
</body>
</html>

word.py

print("Hello, world!")

範例二、兩數相加算術

範例程式

index.php

<!DOCTYPE html>
<html>
<head>
	<title>兩數相加算術範例</title>
</head>
<body>
	<h2>兩數相加算術</h2>
	<form method="POST">
		<label>請輸入第一個數字:</label>
		<input type="text" name="num1">
		<br><br>
		<label>請輸入第二個數字:</label>
		<input type="text" name="num2">
		<br><br>
		<input type="submit" name="submit" value="計算">
	</form>
	<?php
		if(isset($_POST['submit'])){
			$num1 = $_POST['num1'];
			$num2 = $_POST['num2'];

			$python_path = "/usr/bin/python";	// 設定 Python 可執行文件的路徑
			$python_script = "calculate.py";	// 要執行的 Python 腳本路徑

			// 執行 Python 腳本,並傳遞數字參數
			$command = escapeshellcmd("$python_path $python_script $num1 $num2");
			$output = shell_exec($command);

			echo "<p>答案:$output</p>";		// 在網頁上顯示 Python 腳本的輸出
		}
	?>
</body>
</html>

calculate.py


import sys

# 獲取從 PHP 傳遞的兩個數字參數
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])

# 執行算術操作
result = num1 + num2

# 將結果返回給 PHP
print(result)