claudeamadu/firebase

There is no license information available for the latest version (v1.0.2) of this package.

Simplified Firebase Classes for your PHP projects Straight forward codes to easily use in your projects without hassle

v1.0.2 2024-05-28 00:16 UTC

This package is auto-updated.

Last update: 2025-05-29 01:49:00 UTC


README

Simplified Firebase Classes for your PHP projects Straight forward codes to easily use in your projects without hassle

Installation

composer require claudeamadu/firebase

List Classes

FirebaseAuth
FirebaseUser
FirebaseFirestore
FirestoreDB
Query
FieldFilter
CompositeFilter
UnaryFilter
PathBuilder
FirebaseCloudMessaging
Autoload
require 'vendor/autoload.php';

Firebase Auth

Sign in anonymously
$apiKey = 'AIzaSyxxxxxxxxxxxxxxxxxxxx';
$auth = new FirebaseAuth($apiKey);
$token = null;
if ($auth->signInAnonymously()) {
    $token = $auth->getAccessToken()
}
Sign in with email and password
$apiKey = 'AIzaSyxxxxxxxxxxxxxxxxxxxx';
$auth = new FirebaseAuth($apiKey);
$token = null;
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
if ($auth->signIn($email, $password)) {
    $token = $auth->getAccessToken()
}
Sign up with email and password
$apiKey = 'AIzaSyxxxxxxxxxxxxxxxxxxxx';
$auth = new FirebaseAuth($apiKey);
$token = null;
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
if ($auth->signUp($email, $password)) {
    $token = $auth->getAccessToken()
}
Firebase user
$user = $auth->currentUser();

Firestore

Setting up
$Database = '(default)';
$ProjectID = 'xxxxxxxxxxx';
// Create an instance of FirebaseFirestore
$firestore = new FirebaseFirestore($token, $Database, $ProjectID);
$db = $firestore->db;
Getting a document
$documentPath = 'users/xxxxxxx@ttu.edu.gh'; 
$document = $db->getDocument('users', $documentPath);
if ($document !== null) {
    echo $document;
}
Updating a document
$data = [
    "user_name" => "WOW 2",    
    "user_id" => "22195",    
];
$db->updateDocument('users','22195',$data);
Fetch a collection
$collectionPath = 'users'; // Replace with your collection path
$collection = $db->getCollection($collectionPath);
if ($collection !== null) {
    echo $collection;
}
Run a query
$fieldFilter = new FieldFilter();
$compositeFilter = new CompositeFilter();

$fieldFilter->equalTo('user_name', 'WOW 2');

$query = $db->query;
$query->from('users')->where2($fieldFilter);
echo $query->run();

Cloud Messaging

<?php
// Usage:
$credentialsPath = 'serviceAccountCredentials.json';
$topics = 'ios_general';
$title = 'Title of Notification';
$body = 'Body of Notification';
$fcm = new FirebaseCloudMessaging($credentialsPath);

$accessToken = $fcm->getAccessToken();
//echo $accessToken;
$response = $fcm->sendFCMNotificationToTopic($accessToken, $topics, $title, $body);
echo $response;