How To Upload A Json File To Firebase Firestore Using Php

With Google, PHP needs some implementation set up to get your system to work correctly with Google cloud products. Here is a link for you to follow to set up your system to be compatible to run this PHP class. https://cloud.google.com/php/grpc#php-implementation There you will find the setup for various systems, windows, Mac Os, etc. Follow the setup implementation that matches your computer system. If your system is set and ready: We used the PHP package manager composer. So make sure you have composer installed and running on your system. That way we are able to have access to the Firestore client. Require the following packages:

1
2
composer require "grpc/grpc:^v1.27.0"
composer require google/cloud-firestore

Firebase config file required:

Go to your firebase cloud console project settings and download the service account JSON file you will need in order for your system project to connect to your Firestore backend Below is the PHP Class:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
<?php
require('vendor/autoload.php');
#https://cloud.google.com/php/grpc#php-implementation
# Follow the implementation instructions in the above link 
# to get grpc installed and running in your system
# is not you won't be able to get cloud-firestore
# installed with composer
#composer require "grpc/grpc:^v1.27.0"
# composer require google/cloud-firestore
use Google\Cloud\Firestore\FirestoreClient;

class UploadJSONFILEFIRESTORE
{
    protected $db;

    public $json_file_path;
    public $method;
    public $collectionname;

    public $data = array();

    public function __construct()
    {
        # Get execution start time
        $this->time_start = microtime(true);

        #  INIT / CONFIG Firebase
        $this->db = new FirestoreClient([
            "projectId" => "otcollect-demos",
            "keyFile" => json_decode(file_get_contents('./../python/service-account.json'), true)
        ]);

        # Check is argv length is 4
        if (count($_SERVER['argv']) != 4) {
            throw new Exception('ARGV EXCEPTION: Check your command line arguements!');
        }

        # GET COMMAND LINE ARGv AND ASSIGN TO INSTANCE VARIABLES
        $this->json_file_path  = $_SERVER['argv'][1];
        $this->method          = $_SERVER['argv'][2];
        $this->collectionname  = $_SERVER['argv'][3];


        # check to make sure method is either set or add
        # the contructor may not be the exact place for this 
        # control structures, but this is a simple class
        # so there is no much appetide here for writting alot of code here
        # getters and setters are not really that waranted here.

        if ($this->method != 'set' && $this->method != 'add') {
            throw new Exception('WRONG ARGUEMENT EXCEPTION: set or add are only acceptable methods!');
        }
    }

    # Main class method
    # process JSON DATA
    # Uploads data to firestore backend
    public function upload()
    {

        # Read Json file
        $file = file_get_contents($this->json_file_path);

        # Decode Json string to associative array
        $this->data = json_decode($file, true);

        $i = 0;
        foreach ($this->data as $key => $value) {
            # Upload based on method
            print_r($value);
            if ($this->method == 'set') {
                $this->set($value);
            } elseif ($this->method == 'add') {
                $this->add($value);
            }

            # Check if this is last item to be uploaded
            # Log a success message of uploads
            if ($i == count($this->data) - 1) {
                echo '**************************' . "\r\n" . '****SUCCESS UPLOAD*****' . "\r\n" . '**************************' . "\r\n" . '';
                $time_end = microtime(true);
                echo ('Time taken ' . number_format((float) $time_end - $this->time_start, 3) . ' secs');
            }
            $i++;
        }
    }

    # Adds a collection to firestore
    # Firebase Firestore auto-generated ids
    public function add($value)
    {
        $docRef = $this->db->collection($this->collectionname);
        $docRef->add($value);
    }

    # Sets documents to a collection on firestore
    # Uses custom IDS, in this case our json object ID field
    public function set($value)
    {
        $docRef = $this->db->collection($this->collectionname)->document($value['id']);
        $docRef->set($value);
    }
}

$uploadjsonfirestore = new UploadJSONFILEFIRESTORE;
$uploadjsonfirestore->upload();

# php json-firestore/upload-json-firestore.php json-firestore/data.json set demo-users
?>

Git Code: https://github.com/ericel/tutoring/tree/main/php/json-firestore.If you have any questions, do leave a comment below.

Related Posts

0 Comments

12345

    00