JSON PHP
JSON 的一個常見用途是從 Web 伺服器讀取資料,並在網頁中顯示資料。
本章將教你如何在客戶端和 PHP 伺服器之間交換 JSON 資料。
PHP 檔案
PHP 包含一些內建函式來處理 JSON。
可以使用 PHP 函式 json_encode() 將 PHP 中的物件轉換為 JSON
PHP 檔案
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
顯示 PHP 檔案 »
客戶端 JavaScript
這是一個客戶端的 JavaScript,它使用 AJAX 呼叫從上面的示例中請求 PHP 檔案
示例
使用 JSON.parse() 將結果轉換為 JavaScript 物件
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
xmlhttp.open("GET", "demo_file.php");
xmlhttp.send();
自己動手試一試 »
PHP 陣列
使用 PHP 函式 json_encode() 時,PHP 中的陣列也會被轉換為 JSON
PHP 檔案
<?php
$myArr = array("John", "Mary", "Peter", "Sally");
$myJSON = json_encode($myArr);
echo $myJSON;
?>
顯示 PHP 檔案 »
客戶端 JavaScript
這是一個客戶端的 JavaScript,它使用 AJAX 呼叫從上面的陣列示例中請求 PHP 檔案
示例
使用 JSON.parse() 將結果轉換為 JavaScript 陣列
var xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj[2];
}
xmlhttp.open("GET", "demo_file_array.php", true);
xmlhttp.send();
自己動手試一試 »
PHP 資料庫
PHP 是一種伺服器端程式語言,可用於訪問資料庫。
假設你的伺服器上有一個數據庫,你想從客戶端向它傳送一個請求,要求返回表 "customers" 的前 10 行。
在客戶端,建立一個 JSON 物件來描述你想返回的行數。
在將請求傳送到伺服器之前,將 JSON 物件轉換為字串,並將其作為引數傳送到 PHP 頁面的 URL。
示例
使用 JSON.stringify() 將 JavaScript 物件轉換為 JSON
const limit = {"limit":10};
const dbParam = JSON.stringify(limit);
xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xmlhttp.open("GET","json_demo_db.php?x=" + dbParam);
xmlhttp.send();
自己動手試一試 »
示例說明
- 定義一個包含 "limit" 屬性和值的物件。
- 將物件轉換為 JSON 字串。
- 向 PHP 檔案傳送請求,並將 JSON 字串作為引數。
- 等待請求返回結果(以 JSON 格式)
- 顯示從 PHP 檔案接收到的結果。
檢視 PHP 檔案
PHP 檔案
<?php
header("Content-Type: application/json; charset=UTF-8");
$obj = json_decode($_GET["x"], false);
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$stmt = $conn->prepare("SELECT name FROM customers LIMIT ?");
$stmt->bind_param("s", $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>
PHP 檔案解釋
- 使用 PHP 函式 json_decode() 將請求轉換為物件。
- 訪問資料庫,並用請求的資料填充一個數組。
- 將陣列新增到物件中,並使用 json_encode() 函式將物件作為 JSON 返回。
使用資料
示例
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
let text = "";
for (let x in myObj) {
text += myObj[x].name + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
自己動手試一試 »
PHP 方法 = POST
在向伺服器傳送資料時,通常最好使用 HTTP POST
方法。
要使用 POST
方法傳送 AJAX 請求,請指定方法和正確的標頭。
傳送到伺服器的資料現在必須是 send()
方法的引數
示例
const dbParam = JSON.stringify({"limit":10});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
let text ="";
for (let x in myObj) {
text += myObj[x].name + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
xmlhttp.open("POST", "json_demo_db_post.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("x=" + dbParam);
自己動手試一試 »
PHP 檔案中唯一的區別是獲取傳輸資料的**方法**。
PHP 檔案
使用 $_POST 而不是 $_GET
<?php
header("Content-Type: application/json; charset=UTF-8");
$obj = json_decode($_POST["x"], false);
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$stmt = $conn->prepare("SELECT name FROM customers LIMIT ?");
$stmt->bind_param("s", $obj->limit);
$stmt->execute();
$result = $stmt->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp);
?>