PHP code example of ahsankhatri / firestore-php

1. Go to this page and download the library: Download ahsankhatri/firestore-php 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/ */

    

ahsankhatri / firestore-php example snippets


$firestoreClient = new FirestoreClient('project-id', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', [
    'database' => '(default)',
]);

$firestoreClient->addDocument($collection, [
    'booleanTrue' => true,
    'booleanFalse' => false,
    'null' => null,
    'string' => 'abc123',
    'integer' => 123456,
    'arrayRaw' => [
        'string' => 'abc123',
    ],
    'bytes' => new FirestoreBytes('bytesdata'),
    'array' => new FirestoreArray([
        'string' => 'abc123',
    ]),
    'reference' => new FirestoreReference('/users/23'),
    'object' => new FirestoreObject(['nested1' => new FirestoreObject(['nested2' => new FirestoreObject(['nested3' => 'test'])])]),
    'timestamp' => new FirestoreTimestamp,
    'geopoint' => new FirestoreGeoPoint(1,1),
]);

$document = new FirestoreDocument;
$document->setObject('sdf', new FirestoreObject(['nested1' => new FirestoreObject(['nested2' => new FirestoreObject(['nested3' => 'test'])])]));
$document->setBoolean('booleanTrue', true);
$document->setBoolean('booleanFalse', false);
$document->setNull('null', null);
$document->setString('string', 'abc123');
$document->setInteger('integer', 123456);
$document->setArray('arrayRaw', ['string'=>'abc123']);
$document->setBytes('bytes', new FirestoreBytes('bytesdata'));
$document->setArray('arrayObject', new FirestoreArray(['string' => 'abc123']));
$document->setTimestamp('timestamp', new FirestoreTimestamp);
$document->setGeoPoint('geopoint', new FirestoreGeoPoint(1.11,1.11));

$firestoreClient->addDocument($collection, $document, 'customDocumentId');

$document->fillValues([
    'string' => 'abc123',
    'boolean' => true,
]);

$firestoreClient->updateDocument($documentRoot, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
]);

$firestoreClient->updateDocument($documentPath, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], true);

$firestoreClient->setDocument($collection, $documentId, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], [
    'exists' => true, // Indicate document must exist
]);

$collection = 'collection/document/innerCollection';
$firestoreClient->deleteDocument($collection, $documentId);

$collections = $firestoreClient->listDocuments('users', [
    'pageSize' => 1,
    'pageToken' => 'nextpagetoken'
]);

$document->get('bytes')->parseValue(); // will return bytes decoded value.

// Catch field that doesn't exist in document
try {
    $document->get('allowed_notification');
} catch (\MrShan0\PHPFirestore\Exceptions\Client\FieldNotFound $e) {
    // Set default value
}

$firestoreClient
    ->authenticator()
    ->signInEmailPassword('[email protected]', 'abc123');

$firestoreClient
    ->authenticator()
    ->signInAnonymously();

$authToken = $firestoreClient->authenticator()->getAuthToken();
bash
composer 
javascript
"khatri/firestore-php": "^2.0",
}