xxxxxxxxxx
<?php
$con = mysqli_connect("Localhost", "root", "", "rest_api");
if (mysqli_connect_errno())
{
echo "Connection Fail" . mysqli_connect_error();
}
header('Content-Type:application/json');
if (isset($_GET['token']))
{
$token = mysqli_real_escape_string($con, $_GET['token']);
$checkTokenRes = mysqli_query($con, "select * from api_token where token='$token'");
if (mysqli_num_rows($checkTokenRes) > 0)
{
$checkTokenRow = mysqli_fetch_assoc($checkTokenRes);
if ($checkTokenRow['status'] == 1)
{
if ($checkTokenRow['hit_limit'] <= $checkTokenRow['hit_count'])
{
$status = 'true';
$data = "Api hit limit exceed";
$code = '6';
}
else
{
mysqli_query($con, "UPDATE `api_token` SET `hit_count`= hit_count+1 WHERE `token` = '$token'");
$sql = "select * from collected_data ";
if (isset($_GET['id']) && $_GET['id'] > 0)
{
$id = mysqli_real_escape_string($con, $_GET['id']);
$sql .= " where id='$id'";
}
$sqlRes = mysqli_query($con, $sql);
if (mysqli_num_rows($sqlRes) > 0)
{
$data = [];
while ($row = mysqli_fetch_assoc($sqlRes))
{
$data[] = $row;
}
$status = 'true';
$code = '5';
}
else
{
$status = 'true';
$data = "Data not found";
$code = '4';
}
}
}
else
{
$status = 'true';
$data = "API token deactivated";
$code = '3';
}
}
else
{
$status = 'true';
$data = "Please provide valid API token";
$code = '2';
}
}
else
{
$status = 'true';
$data = "Please provide API token";
$code = '1';
}
echo json_encode(["status" => $status, "data" => $data, "code" => $code]);
?>
xxxxxxxxxx
You should consider looking at
https://developer.okta.com/blog/2019/03/08/simple-rest-api-php#create-the-php-project-skeleton-for-your-rest-api
xxxxxxxxxx
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Max-Age:3600");
header("Access-Control-Allow-Headers:*");
xxxxxxxxxx
<?php
// Include any necessary dependencies here, such as database connection or external libraries
// Example: Database connection
$db = new mysqli("localhost", "username", "password", "database_name");
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
// Example: External library
// include_once "vendor/autoload.php";
// Set headers for CORS and JSON response
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// Example route: /api/users
if ($_SERVER['REQUEST_METHOD'] === 'GET' && $_SERVER['REQUEST_URI'] === '/api/users') {
// Handle GET request for /api/users
// Fetch data from database or any other source
// Format the response as JSON
$response = [
'status' => 'success',
'data' => [
'user1' => [
'id' => 1,
'name' => 'John Doe',
'email' => 'john.doe@example.com'
],
'user2' => [
'id' => 2,
'name' => 'Jane Smith',
'email' => 'jane.smith@example.com'
]
]
];
echo json_encode($response);
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['REQUEST_URI'] === '/api/users') {
// Handle POST request for /api/users
// Get data from request body
$data = json_decode(file_get_contents("php://input"), true);
// Insert data into database or any other operation
// Format the response as JSON
$response = [
'status' => 'success',
'data' => [
'user' => $data
]
];
echo json_encode($response);
} else {
// Handle other routes or invalid requests
$response = [
'status' => 'error',
'message' => 'Invalid API endpoint'
];
echo json_encode($response);
}
xxxxxxxxxx
function make_api_request($url, $method = 'GET', $post_fields = null, $headers = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($method == 'GET') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
}
if ($method == 'POST' && $post_fields) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
curl_close($ch);
return false;
}
curl_close($ch);
return json_decode($response, true);
}
xxxxxxxxxx
<?php
// Define a URL here
$url = "https://www.tutorialspoint.com/search?q=php&lang=en";
// Use base64_encode() function
$encodedUrl = base64_encode($“test” . ‘:’ . “test123”);
echo "Here is the encoded URL: ";
echo $encodedUrl;
?>
xxxxxxxxxx
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include("./config.php");
include("./products.php");
$config=new Confi();
$db=$config->connection();
$product=new products($db);
$data = json_decode(file_get_contents("php://input"));
$products=array();
if(!empty($data->product_name) && !empty($data->product_desc) && !empty($data->price)){
$products['product_name'] = $data->product_name;
$products['product_desc'] = $data->product_desc;
$products['price'] = $data->price;
$products['datetime'] = date('Y-m-d H:m:s');
if($product->adddata($products)){
http_response_code(200);
echo json_encode(array("message" => "Product was created."));
}else{
http_response_code(503);
echo json_encode(array("message" => "Unable to create Product."));
}
}else{
http_response_code(400);
echo json_encode(array("message" => "Data is incomplete, Unable to create Product."));
}
xxxxxxxxxx
Detail description with simple CRUD operation Check it out.
https://programmerdesk.com/how-to-create-rest-api-using-php-and-mysql/
xxxxxxxxxx
# CodewarsBadge
My Codewars Badge:

xxxxxxxxxx
# CodewarsBadge
My Codewars Badge:
