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:
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:
<?phprequire('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-firestoreuseGoogle\Cloud\Firestore\FirestoreClient;classUploadJSONFILEFIRESTORE{protected$db;public$json_file_path;public$method;public$collectionname;public$data=array();publicfunction__construct(){# Get execution start time$this->time_start=microtime(true);# INIT / CONFIG Firebase$this->db=newFirestoreClient(["projectId"=>"otcollect-demos","keyFile"=>json_decode(file_get_contents('./../python/service-account.json'),true)]);# Check is argv length is 4if(count($_SERVER['argv'])!=4){thrownewException('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'){thrownewException('WRONG ARGUEMENT EXCEPTION: set or add are only acceptable methods!');}}# Main class method# process JSON DATA# Uploads data to firestore backendpublicfunctionupload(){# 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->dataas$key=>$value){# Upload based on methodprint_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 uploadsif($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 idspublicfunctionadd($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 fieldpublicfunctionset($value){$docRef=$this->db->collection($this->collectionname)->document($value['id']);$docRef->set($value);}}$uploadjsonfirestore=newUploadJSONFILEFIRESTORE;$uploadjsonfirestore->upload();# php json-firestore/upload-json-firestore.php json-firestore/data.json set demo-users?>