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->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
}