1. Go to this page and download the library: Download bensontrent/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/ */
bensontrent / firestore-php example snippets
use MrShan0\PHPFirestore\FirestoreClient;
$firestoreClient = new FirestoreClient('my-project-id', 'MY-API-KEY-xxxxxxxxxxxxxxxxxxxxxxx', [
'database' => '(default)',
]);
use MrShan0\PHPFirestore\FirestoreClient;
// Optional, depending on your usage
use MrShan0\PHPFirestore\Fields\FirestoreTimestamp;
use MrShan0\PHPFirestore\Fields\FirestoreArray;
use MrShan0\PHPFirestore\Fields\FirestoreBytes;
use MrShan0\PHPFirestore\Fields\FirestoreGeoPoint;
use MrShan0\PHPFirestore\Fields\FirestoreObject;
use MrShan0\PHPFirestore\Fields\FirestoreReference;
use MrShan0\PHPFirestore\Attributes\FirestoreDeleteAttribute;
$collection = 'myCollectionName';
$firestoreClient->addDocument($collection, [
'myBooleanTrue' => true,
'myBooleanFalse' => false,
'null' => null,
'myString' => 'abc123',
'myInteger' => 123456,
'arrayRaw' => [
'string' => 'abc123',
],
'bytes' => new FirestoreBytes('bytesdata'),
'myArray' => new FirestoreArray([
'firstName' => 'Jane',
]),
'reference' => new FirestoreReference('/users/23'),
'myObject' => new FirestoreObject(['nested1' => new FirestoreObject(
['nested2' => new FirestoreObject(
['nested3' => 'test'])
])
]),
'timestamp' => new FirestoreTimestamp,
'geopoint' => new FirestoreGeoPoint(1,1),
]);
$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
}