Created
June 25, 2022 06:58
-
-
Save SpiffGreen/bc5b13d14db58ba3249288598cecb807 to your computer and use it in GitHub Desktop.
API with vanilla php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "./config/db.php"; | |
if ($_SERVER['REQUEST_METHOD'] === "POST") { | |
//Make sure that the content type of the POST request has been set to application/json | |
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; | |
if (strcasecmp($contentType, 'application/json') != 0) { | |
// throw new Exception('Content type must be: application/json'); | |
exit('Content type must be: application/json'); | |
} | |
// receive json data | |
$data = json_decode(file_get_contents('php://input'), true); | |
//If json_decode failed, the JSON is invalid. | |
if (!is_array($data)) { | |
// throw new Exception('Received content contained invalid JSON!'); | |
exit('Received content contained invalid JSON!'); | |
} | |
// insert data into database | |
$sql = 'INSERT INTO feedback (email, summary, rating) VALUES (:email, :summary, :rating)'; | |
$stmt = $pdo->prepare($sql); | |
$stmt->execute([ | |
'email' => $data['email'], | |
'summary' => $data['summary'], | |
'rating' => $data['rating'] | |
]); | |
header('Content-Type: application/json'); | |
echo json_encode([ | |
"success" => true, | |
"message" => "Successfully submitted feedback" | |
]); | |
} else { | |
echo "Sorry expected a POST request"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "./config/db.php"; | |
if ($_SERVER['REQUEST_METHOD'] === "DELETE") { | |
if(!isset($_GET["id"])) exit("Please pass feedback id as query"); | |
$id = $_GET["id"]; | |
// delete data from database | |
$sql = 'DELETE FROM feedback WHERE id = :id'; | |
$stmt = $pdo->prepare($sql); | |
$stmt->execute([ | |
'id' => $id | |
]); | |
header('Content-Type: application/json'); | |
echo json_encode([ | |
"success" => true, | |
"message" => "Successfully deleted feedback" | |
]); | |
} else { | |
echo "Sorry expected a DELETE request"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "./config/db.php"; | |
if ($_SERVER['REQUEST_METHOD'] === "GET") { | |
// Handle GET request | |
$result = null; | |
if(isset($_GET["id"])) { | |
$id = $_GET['id']; | |
$sql = 'SELECT * FROM feedback WHERE id = ?'; | |
$stmt = $pdo->prepare($sql); | |
$stmt->execute([$id]); | |
$result = $stmt->fetchObject(); | |
} else { | |
$sql = 'SELECT * FROM feedback'; | |
$stmt = $pdo->query($sql); | |
$result = $stmt->fetchAll(); | |
} | |
header('Content-Type: application/json'); | |
echo json_encode($result); | |
} else { | |
echo "Sorry expected a GET request"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "./config/db.php"; | |
if ($_SERVER['REQUEST_METHOD'] === "PUT") { | |
//Make sure that the content type of the POST request has been set to application/json | |
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; | |
if (strcasecmp($contentType, 'application/json') != 0) { | |
// throw new Exception('Content type must be: application/json'); | |
exit('Content type must be: application/json'); | |
} | |
// receive json data | |
$data = json_decode(file_get_contents('php://input'), true); | |
//If json_decode failed, the JSON is invalid. | |
if (!is_array($data)) { | |
// throw new Exception('Received content contained invalid JSON!'); | |
exit('Received content contained invalid JSON!'); | |
} | |
// insert data into database | |
$sql = 'UPDATE feedback SET summary = :summary, email = :email WHERE id = :id'; | |
$stmt = $pdo->prepare($sql); | |
$stmt->execute([ | |
'email' => $data['email'], | |
'summary' => $data['summary'], | |
'id' => $data['id'] | |
]); | |
header('Content-Type: application/json'); | |
echo json_encode([ | |
"success" => true, | |
"message" => "Successfully udpated feedback" | |
]); | |
} else { | |
echo "Sorry expected an PUT request"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment