I followed the below online tutorial to build a PHP API.
https://trinitytuts.com/restful-web-services-with-php-mysqli/
The HTTP request works perfectly when I use Chrome's "Advanced Rest Client". I get the following response:
{
"status": 1
"msg": "Your record inserted successfully"
}
I tried to put the following URL directly into my web browser:
http://localhost/API/insert.php/name=paul&email=paul@domain.com&status=active
I expected the same result as I received when I used the Advanced Rest Client however, I received the following error:
Notice: Undefined index: name
I am setting the variable 'name' using the following PHP code:
$name = $_POST['name'];
My understanding is that when I paste the URL into the browser, the $name variable is not set to $_POST['name'] because entering the URL is not actually a post action.
Is it possible for me add a parameter to the URL so as to tell the browser to post? For example:
http://localhost/API/insert.php/type=POST&name=paul&email=paul@domain.com&status=active
In a nutshell, I want to be able to past a URL directly into a web browser and and not have to post it via a HTML form.
This is my PHP code for insert.php
<?php
include 'conn.php';
$name = $_POST['name'];
$email = $_POST['email'];
$status = $_POST['status'];
$id = 1;
$sql = "INSERT INTO `service`.`user` (`id`, `name`, `email`, `status`) VALUES ($id, '$name', '$email', '$status');";
if ($connection->query($sql)) {
$msg = array("status" =>1 , "msg" => "Your record inserted successfully");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
$json = $msg;
header('content-type: application/json');
echo json_encode($json);
@mysqli_close($conn);
?>