Today I will be implementing some HTTP commands to demonstrate Representational State Transfer.
Get, Post, Put, Delete... 4 of the 9 HTTP standard methods.
Applications: Postman and CURL are used to test APIs.
HTTP GET requests cannot send data to the server in the body of a GET message or change the server's state. (But you can still pass data to the server in URL parameters.)
Tutorial 1 - The first site I landed on, nothing special about it. I searched for REST API examples using PHP/MySql.
https://www.geeksforgeeks.org/php/building-a-rest-api-with-php-and-mysql/
Article first covers how to set up PHPMyadmin - that's already done on my site.
I want to read JSON data using GET. As an exercise, I want to see if I can make this
base php file work with cURL, instead of Postman. I'm electing to do this via cURL instead of Postman, as suggested in this article; but i have lifted the php code from the article, to use as a starting point.
Here's my version of it. I've made some changes.
-
I'm *not* includinig 'db.php' because I have my own db file and function to create $conn - line 5.
-
I'm using my Employee table, instead of the given 'employee' table - line 15.
<?php
require_once 'mainfunctions.php';
$conn = createConnChinook();
header("Content-Type: application/json");
$method = $_SERVER['REQUEST_METHOD'];
$input = json_decode(file_get_contents('php://input'), true);
switch ($method) {
case 'GET':
if (isset($_GET['id'])) {
$id = $_GET['id'];
$result = $conn->query("SELECT * FROM employee WHERE EmployeeId=2");
$data = $result->fetch_assoc();
echo json_encode($data);
} else {
$result = $conn->query("SELECT * FROM employee WHERE EmployeeId=1");
$employee = [];
while ($row = $result->fetch_assoc()) {
$employee[] = $row;
}
echo json_encode($employee);
}
break;
case 'POST':
$name = $input['name'];
$email = $input['email'];
$age = $input['age'];
$conn->query("INSERT INTO employee (name, email, age) VALUES ('$name', '$email', $age)");
echo json_encode(["message" => "User added successfully"]);
break;
case 'PUT':
$id = $_GET['id'];
$name = $input['name'];
$email = $input['email'];
$age = $input['age'];
$conn->query("UPDATE employee SET name='$name',
email='$email', age=$age WHERE id=$id");
echo json_encode(["message" => "User updated successfully"]);
break;
case 'DELETE':
$id = $_GET['id'];
$conn->query("DELETE FROM employee WHERE id=$id");
echo json_encode(["message" => "User deleted successfully"]);
break;
default:
echo json_encode(["message" => "Invalid request method"]);
break;
}
$conn->close();
?>
Taking a look at line 7. Im somewhat familiar with header() in my other php files, but not clear on what the intent of it is here.
header("Content-Type: application/json");
It seems like the header() may be there in order to enable the return of JSON data. Great!
The problem here is that curl is an alias for a powershell command, but I'm passing params to it like you would if using the real curl.
Before figuring that out, I thought line breaks were my problem since several examples I saw seem to show a line break.
Trying the backtick thing out..
Also notice that im using curl
.exe since we're in Powershell - which means, we're not running Invoke-WebRequest. Good.
But still I'm not 'pulling JSON data' like I intend to do.
This is simply pulling the HTML of the site.. which is cool to see, but not meeting the goal.
Zooming out again
Going back to Powershell. Here curl works without .exe becuase we're passing a valid parameter.. url with page path.
But this breaks it.
Figuring out my mistake here.
Zoom out.
Trying CMD
Here I am realizing that the sample code does not produce a page. I havent seen code before that causes and HTML page to come across in 'view source' like this.
I fixed it after some trial/error - the header() is what breaks it. Important to keep in mind that the tutorial didnt claim that this produced a readable webpage, so ok fine.
Here's the 'fixed' page, with header() removed. We can see that Employee1 is being read from the db. It's due the else statement, line 61 in the above image. We are accessing the page, GET is not set, else triggers and reads the db row. Fine, but not what we're looking for.
Seeking more info.
Asking a different question:
From CMD, trying some switches and re-ordering the params - filename listed last.
Some of the examples list a folder path after the url - is that required?
Starting over..
Different tutorial and example code.
Still wants to use POSTMAN, but lets see what we can do with cURL...
To be continued.....