claudeamadu / firebase
Simplified Firebase Classes for your PHP projects Straight forward codes to easily use in your projects without hassle
Installs: 10
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=8.0
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;