API NEW
This PHP script api.php is designed to set up an API endpoint using the Xcrud library. It provides a simple way to interact with a database for CRUD (Create, Read, Update, Delete) operations. Here’s a breakdown of what the code does:
HIDE CODE
<?php //Save this file api.php somewhere accessible //$xcrud->table('products'); echo "<br><a href='../api_GET.php'>GET link</a>"; echo "<br><a href='../api_UPDATE.php'>UPDATE link</a>"; echo "<br><a href='../api_DELETE.php'>DELETE link</a>"; echo "<br><a href='../api_PUT.php'>PUT/INSERT link</a>"; $xcrud = Xcrud::get_instance(); $xcrud->table("payments"); $xcrud->columns("customerNumber,checkNumber,amount"); $xcrud->token('key123!');// Set Bearer token for authentication $xcrud->response_message(array("PUT"=>"Successfully inserted record","DELETE"=>"Successfully deleted record","POST"=>"Successfully Updated record"));//response message $xcrud->limit(10);// this applies for GET number of records //$json_data = $xcrud->render('api'); //echo $json_data; //ACCEPTS PUT, GET, POST, UPDATE on api,php //GET /* <?php // URL of the API endpoint $url = 'http://localhost/xCrud_1.7.25.2/api.php'; // Bearer token $bearerToken = 'key123!'; // Initialize cURL session $ch = curl_init($url); // Set the options for cURL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $bearerToken )); // Execute the request $response = curl_exec($ch); // Check for errors if (curl_errno($ch)) { echo 'cURL Error: ' . curl_error($ch); } else { // Output the response echo $response; } // Close the cURL session curl_close($ch); ?> */ //UPDATE /* <?php // URL of the API endpoint $url = 'http://localhost/xCrud_1.7.25.2/api.php'; // Data to be sent in the request body (e.g., primary key, field to update, and new value) $data = array( "primary" => 1, // Example primary key "field" => "amount", // Field to update "value" => 5000 // New value for the field ); $bearerToken = 'key123!'; // Convert data to URL-encoded query string format $data_string = http_build_query($data); // Initialize cURL session $ch = curl_init($url); // Set the options for cURL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // Set the request method to POST curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // Attach the data to the request curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($data_string), 'Authorization: Bearer ' . $bearerToken )); // Execute the request $response = curl_exec($ch); // Check for errors if (curl_errno($ch)) { echo 'cURL Error: ' . curl_error($ch); } else { // Output the response echo $response; } // Close the cURL session curl_close($ch); ?> */ //UPDATE /* <?php // URL of the API endpoint $url = 'http://localhost/xCrud_1.7.25.2/api.php'; // Data to be sent in the request body (e.g., primary key for deletion) $data = array( "primary" => 1 // Example primary key ); $bearerToken = 'key123!'; // Convert data to URL-encoded query string format $data_string = http_build_query($data); // Initialize cURL session $ch = curl_init($url); // Set the options for cURL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // Set the request method to DELETE curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // Attach the data to the request curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($data_string), 'Authorization: Bearer ' . $bearerToken )); // Execute the request $response = curl_exec($ch); // Check for errors if (curl_errno($ch)) { echo 'cURL Error: ' . curl_error($ch); } else { // Output the response echo $response; } // Close the cURL session curl_close($ch); ?> */ //UPDATE /* */ ?>
GET link
UPDATE link
DELETE link
PUT/INSERT link