以下是 Server API 端的程式碼:
$verb = $_SERVER['REQUEST_METHOD']; if($verb == 'GET') { if(isset($_GET['filename'])) { $file_content = file_get_contents($_GET['filename']); echo $file_content; } else { die('ERROR: REQUIRED PARAMETERS NOT GIVEN!'); } } elseif($verb == 'POST') { if(isset($_POST['filename']) and isset($_POST['content'])) { file_put_contents($_POST['filename'], $_POST['content']); } else { die('ERROR: REQUIRED PARAMETERS NOT GIVEN!'); } } elseif($verb == 'DELETE') { parse_str(file_get_contents('php://input'), $_DELETE); if(isset($_DELETE['file'])) { if(file_exists($_DELETE['file'])) { unlink($_DELETE['file']); } } else { die('ERROR: REQUIRED PARAMETERS NOT GIVEN!'); } }
然後我們可以透過 cURL 或 Chrome 的擴充功能 Advanced REST client 來測試上面的 API 程式。