PHP JSON Response API Basics
Ringkasan
Untuk API, PHP mengembalikan JSON: set header Content-Type: application/json, encode array via json_encode, dan atur status code.
Konsep Dasar
header('Content-Type: application/json').echo json_encode($data, JSON_UNESCAPED_UNICODE).http_response_code(201)untuk Created,400bad request, dll.- Baca body request:
json_decode(file_get_contents('php://input'), true).
Cara Kerja / Struktur
header('Content-Type: application/json');
$data = ['id' => 1, 'nama' => 'Budi'];
echo json_encode($data, JSON_UNESCAPED_UNICODE);