Real-time data management and transactions are the latest mode of communication these days. People need quick data flow while using mobile and web applications. There are alot of services available on internet for creating real-time databases and communication systems.
dbcon.php
<?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\Auth;
$factory = (new Factory)
->withServiceAccount('php-firebase-oauth-firebase-adminsdk-sc65a-18f71c3027.json')
->withDatabaseUri('https://php-firebase-oauth-default-rtdb.firebaseio.com/');
$database = $factory->createDatabase();
$auth = $factory->createAuth();
?>
index.php
<?php
header("Content-Type: application/json");
header("Acess-Control-Allow-Origin: *"); // other website is not access this api, if is line is not given
require 'dbcon.php';
$reference = $database->getReference('contacts');
$fetchdata = $reference->getValue();
$i = 0;
if($fetchdata > 0){
echo json_encode([
'status' => 200,
'data' => $fetchdata,
]);
}
else{
echo json_encode([
'status' => 203,
'data' => "No Data Fount",
]);
}
?>
url for GET api
http://localhost/firebase/auth/index.php
index.php
<?php
header("Content-Type: application/json");
header("Acess-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization");
require 'dbcon.php';
$data = json_decode(file_get_contents("php://input"), true);
$name = $data['name'];
$email = $data['email'];
$phone = $data['phone'];
$postData = [
'name' => $name,
'email' => $email,
'phone' => $phone
];
$postRef = $database->getReference('contacts')->push($postData);
if ($postRef) {
echo "Data Saved Successfully !";
}
else{
echo "Failled !";
}
?>
api with data
http://localhost/firebase/auth/index.php
Headers
Content-Type : application/json
raw data:
{
"name":"Bina Darling 2",
"email": "bina2@gmail.com",
"phone": "1234567820"
}