PHP code example of claudeamadu / firebase

1. Go to this page and download the library: Download claudeamadu/firebase library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

claudeamadu / firebase example snippets


FirebaseAuth
FirebaseUser
FirebaseFirestore
FirestoreDB
Query
FieldFilter
CompositeFilter
UnaryFilter
PathBuilder
FirebaseCloudMessaging



$apiKey = 'AIzaSyxxxxxxxxxxxxxxxxxxxx';
$auth = new FirebaseAuth($apiKey);
$token = null;
if ($auth->signInAnonymously()) {
    $token = $auth->getAccessToken()
}

$apiKey = 'AIzaSyxxxxxxxxxxxxxxxxxxxx';
$auth = new FirebaseAuth($apiKey);
$token = null;
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
if ($auth->signIn($email, $password)) {
    $token = $auth->getAccessToken()
}

$apiKey = 'AIzaSyxxxxxxxxxxxxxxxxxxxx';
$auth = new FirebaseAuth($apiKey);
$token = null;
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
if ($auth->signUp($email, $password)) {
    $token = $auth->getAccessToken()
}

$user = $auth->currentUser();

$Database = '(default)';
$ProjectID = 'xxxxxxxxxxx';
// Create an instance of FirebaseFirestore
$firestore = new FirebaseFirestore($token, $Database, $ProjectID);
$db = $firestore->db;

$documentPath = 'users/[email protected]'; 
$document = $db->getDocument('users', $documentPath);
if ($document !== null) {
    echo $document;
}

$data = [
    "user_name" => "WOW 2",    
    "user_id" => "22195",    
];
$db->updateDocument('users','22195',$data);

$collectionPath = 'users'; // Replace with your collection path
$collection = $db->getCollection($collectionPath);
if ($collection !== null) {
    echo $collection;
}

$fieldFilter = new FieldFilter();
$compositeFilter = new CompositeFilter();

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

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


// 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;