使用者:游士賢:修訂版本之間的差異
出自福留子孫
(→php呼叫python顯示於網頁) |
(→php呼叫python顯示於網頁) |
||
| 第 82 行: | 第 82 行: | ||
# 將結果返回給 PHP | # 將結果返回給 PHP | ||
print(result) | print(result) | ||
| + | </pre> | ||
| + | |||
| + | ==php呼叫xml顯示於網頁== | ||
| + | ===範例一、已知兩數,應用php讀取xml數值=== | ||
| + | *說明:xml 檔案已存在兩位數「12」和「34」,應用php讀取xml檔,將答案將顯示於網頁上。 | ||
| + | *範例程式:[http://jendo.org/~游士賢/chatgcp/06 連結] | ||
| + | |||
| + | '''index.php,程式碼如下:''' | ||
| + | <pre> | ||
| + | <?php | ||
| + | // 讀取 XML 檔案 | ||
| + | $xml = simplexml_load_file("numbers.xml"); | ||
| + | |||
| + | // 取出兩個數字 | ||
| + | $num1 = (int)$xml->number1; | ||
| + | $num2 = (int)$xml->number2; | ||
| + | |||
| + | // 計算結果 | ||
| + | $result = $num1 + $num2; | ||
| + | |||
| + | // 回傳結果 | ||
| + | echo "$num1 + $num2 = $result"; | ||
| + | ?> | ||
| + | </pre> | ||
| + | |||
| + | '''numbers.xml,程式碼如下:''' | ||
| + | <pre> | ||
| + | <?xml version="1.0" encoding="UTF-8"?> | ||
| + | <numbers> | ||
| + | <number1>12</number1> | ||
| + | <number2>34</number2> | ||
| + | </numbers> | ||
| + | </pre> | ||
| + | |||
| + | ===範例二、未知兩數,應用php讀取xml數值=== | ||
| + | *說明:使用者自行輸入兩個數字進行加法運算,按下計算按鈕後,系統會顯示計算結果,同時將計算紀錄存入 XML 檔案中。 | ||
| + | *範例程式:[http://jendo.org/~游士賢/chatgcp/05 連結] | ||
| + | |||
| + | '''index.php,程式碼如下:''' | ||
| + | <pre> | ||
| + | <!DOCTYPE html> | ||
| + | <html> | ||
| + | <head> | ||
| + | <meta charset="UTF-8"> | ||
| + | <title>PHP 結合 XML 的算術範例程式</title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <h1>請輸入兩個數字進行運算</h1> | ||
| + | <form action="calculate.php" method="post"> | ||
| + | <label for="num1">第一個數字:</label> | ||
| + | <input type="number" name="num1" id="num1"><br><br> | ||
| + | <label for="num2">第二個數字:</label> | ||
| + | <input type="number" name="num2" id="num2"><br><br> | ||
| + | <label for="operator">運算符號:</label> | ||
| + | <select name="operator" id="operator"> | ||
| + | <option value="+">+</option> | ||
| + | <option value="-">-</option> | ||
| + | </select><br><br> | ||
| + | <input type="submit" value="計算"> | ||
| + | </form> | ||
| + | </body> | ||
| + | </html> | ||
| + | </pre> | ||
| + | |||
| + | '''calculate.php,程式碼如下:''' | ||
| + | <pre> | ||
| + | <?php | ||
| + | // 讀取 XML 檔案 | ||
| + | $xml = simplexml_load_file('result.xml'); | ||
| + | |||
| + | // 取得使用者輸入的數字和運算符號 | ||
| + | $num1 = $_POST['num1']; | ||
| + | $num2 = $_POST['num2']; | ||
| + | $operator = $_POST['operator']; | ||
| + | |||
| + | // 計算結果 | ||
| + | if ($operator == '+') { | ||
| + | $result = $num1 + $num2; | ||
| + | } else { | ||
| + | $result = $num1 - $num2; | ||
| + | } | ||
| + | |||
| + | // 建立新的計算紀錄 | ||
| + | $calculation = $xml->addChild('calculation'); | ||
| + | $calculation->addChild('num1', $num1); | ||
| + | $calculation->addChild('num2', $num2); | ||
| + | $calculation->addChild('operator', $operator); | ||
| + | $calculation->addChild('result', $result); | ||
| + | |||
| + | // 將結果存入 XML 檔案 | ||
| + | $xml->asXML('result.xml'); | ||
| + | |||
| + | // 顯示計算結果 | ||
| + | echo "計算結果:$num1 $operator $num2 = $result"; | ||
| + | ?> | ||
| + | </pre> | ||
| + | |||
| + | '''result.xml,程式碼如下:''' | ||
| + | <pre> | ||
| + | <?xml version="1.0" encoding="UTF-8"?> | ||
| + | <calculations> | ||
| + | </calculations> | ||
</pre> | </pre> | ||
2023年3月15日 (三) 14:24的修訂版本
目錄
php呼叫python顯示於網頁
範例一、文字顯示
- 說明:利用 php 讀取 python 檔案,並將文字顯示「Hello, world!」於頁面上。
- 範例程式:連結
index.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!")
範例二、兩數相加算術
- 說明:利用 php 讀取 python 檔案,並使用者將兩個數值填入並按計算,答案將顯示於網頁上。
- 範例程式:連結
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)
php呼叫xml顯示於網頁
範例一、已知兩數,應用php讀取xml數值
- 說明:xml 檔案已存在兩位數「12」和「34」,應用php讀取xml檔,將答案將顯示於網頁上。
- 範例程式:連結
index.php,程式碼如下:
<?php
// 讀取 XML 檔案
$xml = simplexml_load_file("numbers.xml");
// 取出兩個數字
$num1 = (int)$xml->number1;
$num2 = (int)$xml->number2;
// 計算結果
$result = $num1 + $num2;
// 回傳結果
echo "$num1 + $num2 = $result";
?>
numbers.xml,程式碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<numbers>
<number1>12</number1>
<number2>34</number2>
</numbers>
範例二、未知兩數,應用php讀取xml數值
- 說明:使用者自行輸入兩個數字進行加法運算,按下計算按鈕後,系統會顯示計算結果,同時將計算紀錄存入 XML 檔案中。
- 範例程式:連結
index.php,程式碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP 結合 XML 的算術範例程式</title>
</head>
<body>
<h1>請輸入兩個數字進行運算</h1>
<form action="calculate.php" method="post">
<label for="num1">第一個數字:</label>
<input type="number" name="num1" id="num1"><br><br>
<label for="num2">第二個數字:</label>
<input type="number" name="num2" id="num2"><br><br>
<label for="operator">運算符號:</label>
<select name="operator" id="operator">
<option value="+">+</option>
<option value="-">-</option>
</select><br><br>
<input type="submit" value="計算">
</form>
</body>
</html>
calculate.php,程式碼如下:
<?php
// 讀取 XML 檔案
$xml = simplexml_load_file('result.xml');
// 取得使用者輸入的數字和運算符號
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$operator = $_POST['operator'];
// 計算結果
if ($operator == '+') {
$result = $num1 + $num2;
} else {
$result = $num1 - $num2;
}
// 建立新的計算紀錄
$calculation = $xml->addChild('calculation');
$calculation->addChild('num1', $num1);
$calculation->addChild('num2', $num2);
$calculation->addChild('operator', $operator);
$calculation->addChild('result', $result);
// 將結果存入 XML 檔案
$xml->asXML('result.xml');
// 顯示計算結果
echo "計算結果:$num1 $operator $num2 = $result";
?>
result.xml,程式碼如下:
<?xml version="1.0" encoding="UTF-8"?> <calculations> </calculations>