PHPackages                             bensontrent/firestore-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. bensontrent/firestore-php

ActiveLibrary[Database &amp; ORM](/categories/database)

bensontrent/firestore-php
=========================

Firestore PHP Client without gRPC and support for Guzzle 7. Forked from archived project ahsankhatri/firestore-php

3.1.0(2y ago)2190.0k↓20.9%7MITPHPPHP &gt;=7.3

Since Mar 23Pushed 1y ago2 watchersCompare

[ Source](https://github.com/bensontrent/firestore-php)[ Packagist](https://packagist.org/packages/bensontrent/firestore-php)[ Docs](https://github.com/bensontrent/firestore-php)[ RSS](/packages/bensontrent-firestore-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (6)Used By (0)

Firestore Client for PHP without gRPC
=====================================

[](#firestore-client-for-php-without-grpc)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d37a443ad677bebc7c463908fd40b608034b5ddd29570c00567b10ea3a48fd45/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62656e736f6e7472656e742f6669726573746f72652d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bensontrent/firestore-php)[![Total Installs](https://camo.githubusercontent.com/18be8f4df351c916aa1e20ca31b7977f2a379a12a3d1f25a5b491e34bd36dac9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62656e736f6e7472656e742f6669726573746f72652d7068703f636f6c6f723d677265656e266c6162656c3d696e7374616c6c73)](https://packagist.org/packages/bensontrent/firestore-php)[![Total Downloads](https://camo.githubusercontent.com/c77cdaa7e7386cdb25c17783725e4f21955c83c20fdd30c5a5d8e6b8a352419b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f646f776e6c6f6164732f62656e736f6e7472656e742f6669726573746f72652d7068702f746f74616c3f636f6c6f723d677265656e266c6162656c3d646f776e6c6f616473)](https://github.com/bensontrent/firestore-php)[![License](https://camo.githubusercontent.com/3f3f6b67065942f71c39f010c601bedbf757d0d86b9d623e251813ac9d2f8593/68747470733a2f2f706f7365722e707567782e6f72672f62656e736f6e7472656e742f6669726573746f72652d7068702f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/bensontrent/firestore-php)

Use Google Firebase without the requirement of having the gRPC extension for php installed. This is ideal for shared hosting environments. This package is totally based on [Firestore REST API](https://firebase.google.com/docs/firestore/use-rest-api)

Authentication / Generate API Key
---------------------------------

[](#authentication--generate-api-key)

1. Visit [Google Cloud Firestore API](https://console.cloud.google.com/projectselector/apis/api/firestore.googleapis.com/overview)
2. Select your desired project.
3. Select `Credentials` from left menu and select `API Key` from Server key or `Create your own credentials`

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require bensontrent/firestore-php
```

or install it by adding it to `composer.json` then run `composer update`

```
"require": {
    "bensontrent/firestore-php": "^3.0",
}
```

Dependencies
------------

[](#dependencies)

- PHP 7.3 and above (PHP 8+ supported)

The bindings require the following extensions in order to work properly:

- [`curl`](https://secure.php.net/manual/en/book.curl.php)
- [`json`](https://secure.php.net/manual/en/book.json.php)
- [`guzzlehttp/guzzle`](https://packagist.org/packages/guzzlehttp/guzzle)

If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.

Usage
-----

[](#usage)

#### Initialization

[](#initialization)

```
require 'vendor/autoload.php';

use MrShan0\PHPFirestore\FirestoreClient;

$firestoreClient = new FirestoreClient('my-project-id', 'MY-API-KEY-xxxxxxxxxxxxxxxxxxxxxxx', [
    'database' => '(default)',
]);
```

Note: You likely won't need to change the `'database' => '(default)'` line.

#### Adding a document

[](#adding-a-document)

Make sure your Firebase Rules allow you to write to the $collection you wish to modify or you will get an error: `You do not have permission to access the requested resource`

```
require 'vendor/autoload.php';

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),
]);
```

**NOTE:** Pass third argument if you want your custom **document id** to set else auto-id will generate it for you. For example:

```
$firestoreClient->addDocument('customers', [
    'firstName' => 'Jeff',
], 'myOptionalUniqueID0123456789')
```

Or

```
use MrShan0\PHPFirestore\FirestoreDocument;

$document = new FirestoreDocument;
$document->setObject('myNestedObject', new FirestoreObject(
    ['nested1' => new FirestoreObject(
        ['nested2' => new FirestoreObject(
            ['nested3' => 'test'])
            ])
        ]
    ));
$document->setBoolean('myBooleanTrue', true);
$document->setBoolean('myBooleanFalse', false);
$document->setNull('null', null);
$document->setString('myString', 'abc123');
$document->setInteger('myInteger', 123456);
$document->setArray('myArrayRaw', ['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');
```

And..

```
$document->fillValues([
    'myString' => 'abc123',
    'myBoolean' => true,
    'firstName' => 'Jane',
]);
```

#### Special characters in the field name

[](#special-characters-in-the-field-name)

If you want to use special characters in the field name, you have to use backticks.

```
$document->fillValues([
    '`teléfono`' => '1234567890',
    '`contraseña`' => 'secretPassword',
]);
```

You could use `addNestedDocuments` if you have multiple nested objects

```
require 'vendor/autoload.php';

use MrShan0\PHPFirestore\FirestoreClient;

$collection = 'myCollectionName';

$originalData = [
    'name' => 'My Application',
    'emails' => [
        'support' => 'support@example.com',
        'sales' => 'sales@example.com',
    ],
    'website' => 'https://app.example.com',
    'myObject' => [
        'nested1' => [
            'name' => 'My Application',
            'emails' => [
                'support' => 'support@example.com',
                'sales' => 'sales@example.com',
            ],
            'nested2' => [
                'name' => 'My Application',
                'emails' => [
                    'support' => 'support@example.com',
                    'sales' => 'sales@example.com',
                ],
                'nested3' => [
                    'name' => 'My Application',
                    'emails' => [
                        'support' => 'support@example.com',
                        'sales' => 'sales@example.com',
                    ]
                ]
            ]
        ]
    ]
];

$firestoreClient->addNestedDocuments($collection, $originalData);
```

This just formats the array before using the `addDocument` function

```
require 'vendor/autoload.php';

use MrShan0\PHPFirestore\FirestoreClient;
use MrShan0\PHPFirestore\Fields\FirestoreObject;

$collection = 'myCollectionName';

$originalData = [
    'name' => 'My Application',
    'emails' => new FirestoreObject( [
        'support' => 'support@example.com',
        'sales' => 'sales@example.com',
    ]),
    'website' => 'https://app.example.com',
    'myObject' => new FirestoreObject([
        'nested1' => new FirestoreObject([
            'name' => 'My Application',
            'emails' => new FirestoreObject([
                'support' => 'support@example.com',
                'sales' => 'sales@example.com',
            ]),
            'nested2' => new FirestoreObject([
                'name' => 'My Application',
                'emails' => new FirestoreObject([
                    'support' => 'support@example.com',
                    'sales' => 'sales@example.com',
                ]),
                'nested3' => new FirestoreObject([
                    'name' => 'My Application',
                    'emails' => new FirestoreObject( [
                        'support' => 'support@example.com',
                        'sales' => 'sales@example.com',
                    ])
                ])
            ])
        ])
    ])
];
```

#### Inserting/Updating a document

[](#insertingupdating-a-document)

- Update (Merge) or Insert document

Following will merge document (if exist) else insert the data.

```
use MrShan0\PHPFirestore\Attributes\FirestoreDeleteAttribute;

$firestoreClient->updateDocument($documentRoot, [
    'newFieldToAdd' => 'Jane Doe',
    'existingFieldToRemove' => new FirestoreDeleteAttribute
]);
```

**NOTE:** Passing 3rd argument as a boolean *true* will force check that document must exist and vice-versa in order to perform update operation.

For example: If you want to update document only if exist else `MrShan0\PHPFirestore\Exceptions\Client\NotFound` (Exception) will be thrown.

```
use MrShan0\PHPFirestore\Attributes\FirestoreDeleteAttribute;

$firestoreClient->updateDocument($documentPath, [
    'newFieldToAdd' => 'Jane Doe',
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], true);
```

format for documentPath:

```
/

```

- Overwirte or Insert document

```
use MrShan0\PHPFirestore\Attributes\FirestoreDeleteAttribute;

$firestoreClient->setDocument($collection, $documentId, [
    'newFieldToAdd' => 'Jane Doe',
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], [
    'exists' => true, // Indicate document must exist
]);
```

#### Deleting a document

[](#deleting-a-document)

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

#### List documents with pagination (or custom parameters)

[](#list-documents-with-pagination-or-custom-parameters)

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

**Note:** You can pass custom parameters as supported by [firestore list document](https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/list#query-parameters)

#### Get field from document

[](#get-field-from-document)

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

### Firebase Authentication

[](#firebase-authentication)

#### Sign in with Email and Password.

[](#sign-in-with-email-and-password)

```
$firestoreClient
    ->authenticator()
    ->signInEmailPassword('testuser@example.com', 'abc123');
```

#### Sign in Anonymously.

[](#sign-in-anonymously)

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

### Retrieve Auth Token

[](#retrieve-auth-token)

```
$authToken = $firestoreClient->authenticator()->getAuthToken();
```

### TODO

[](#todo)

- Added delete attribute support.
- Add Support for Object, Boolean, Null, String, Integer, Array, Timestamp, GeoPoint, Bytes
- Add Exception Handling.
- List all documents.
- List all collections.
- Filters and pagination support.
- Structured Query support.
- Transaction support.
- Indexes support.
- Entire collection delete support.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please use the issue tracker.

Credits
-------

[](#credits)

- [Ahsaan Muhammad Yousuf](https://ahsaan.me)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~15 days

Total

5

Last Release

1092d ago

Major Versions

2.0.0 → 3.0.02023-03-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/23b4906cef7e04b263e4bb803af88f6c4d95f893483f347bb66d9840dbe0e26e?d=identicon)[bensontrent](/maintainers/bensontrent)

---

Top Contributors

[![bensontrent](https://avatars.githubusercontent.com/u/5982592?v=4)](https://github.com/bensontrent "bensontrent (30 commits)")[![ahsankhatri](https://avatars.githubusercontent.com/u/7288116?v=4)](https://github.com/ahsankhatri "ahsankhatri (4 commits)")[![backendrulz](https://avatars.githubusercontent.com/u/817816?v=4)](https://github.com/backendrulz "backendrulz (3 commits)")[![p-blomberg](https://avatars.githubusercontent.com/u/332457?v=4)](https://github.com/p-blomberg "p-blomberg (2 commits)")[![edruid](https://avatars.githubusercontent.com/u/384380?v=4)](https://github.com/edruid "edruid (2 commits)")[![cristirusu](https://avatars.githubusercontent.com/u/1969488?v=4)](https://github.com/cristirusu "cristirusu (1 commits)")[![afif-malghani](https://avatars.githubusercontent.com/u/100247208?v=4)](https://github.com/afif-malghani "afif-malghani (1 commits)")[![mowcixo](https://avatars.githubusercontent.com/u/47653?v=4)](https://github.com/mowcixo "mowcixo (1 commits)")[![Baha2Odeh](https://avatars.githubusercontent.com/u/2823958?v=4)](https://github.com/Baha2Odeh "Baha2Odeh (1 commits)")

---

Tags

phpgooglefirebasefirestore

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bensontrent-firestore-php/health.svg)

```
[![Health](https://phpackages.com/badges/bensontrent-firestore-php/health.svg)](https://phpackages.com/packages/bensontrent-firestore-php)
```

###  Alternatives

[kreait/firebase-php

Firebase Admin SDK

2.4k39.7M72](/packages/kreait-firebase-php)[kreait/firebase-bundle

Symfony Bundle for the Firebase Admin SDK

1534.7M2](/packages/kreait-firebase-bundle)[paragraph1/php-fcm

PHP application server for google firebase cloud messaging (FCM)

1991.2M10](/packages/paragraph1-php-fcm)[tomwalder/php-gds

Google Cloud Datastore Library for PHP. Also Firestore in Datastore mode.

161174.0k5](/packages/tomwalder-php-gds)[redjanym/php-firebase-cloud-messaging

PHP SDK for Firebase Cloud Messaging from Google

39847.9k1](/packages/redjanym-php-firebase-cloud-messaging)[morrislaptop/firestore-php

Firestore SDK for PHP without gRPC

6928.6k1](/packages/morrislaptop-firestore-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
