Skip to content

Latest commit

 

History

History
executable file
·
228 lines (155 loc) · 5.97 KB

README.md

File metadata and controls

executable file
·
228 lines (155 loc) · 5.97 KB

PHP7 Firebase REST and STREAM Client

Scrutinizer Code Quality Travis CI Build Status codecov

PHP 7 ready Total Downloads Latest Stable Version License

Based on the Firebase REST API.

Available on Packagist.

###Prerequisites

  • PHP >= 7.0
  • Firebase Active Account
  • Composer (recommended, not required)

Adding Firebase PHP to your project using Composer

cd <your_project>

composer require zend_firebase/zend_firebase dev-master

More info about Composer at getcomposer.org.

Simple Example of Usage

use ZendFirebase\Firebase, ZendFirebase\Authentication\FirebaseAuth;

$auth = new FirebaseAuth();

$auth->setBaseURI('https://your_url_from_firebase/');
$auth->setServertoken('your_firebase_token');

/* ---  EXAMPLE OF DATA TO POST REMEMBER ALL DATA MUST BE ARRAY --- */
$test = array(
    "name" => "TEST",
    "id" => 5245,
    "text" => "hello TEST 5245",
    "status" => "sended"
);

/* --- CREATE NEW OBJECT AND PASS CREDENTIAL --- */
$firebase = new Firebase($auth);


/* --- CHOOCE THE OPERATION (SAME NAME OF FIREBASE DOCS)  --- */
$firebase->post('path', $test);

Inside folder "examples" you can find some another simple complete example for library usage. Go to next step!

Response Usage

/* --- FIREBASE DATA FROM REALTIME DB IS AN ARRAY  --- */
$firebase->getFirebaseData(); 	// <- array, data returning from Firebase
echo $firebase->getOperation(); // <- string, operation just made (for example: GET or POST etc...)
echo $firebase->getStatus(); 	// <- numeric, status of request (for example: 200 or 400 or 500)

Go to next step!

Get Last Auto-Increment Id generate from Firebase after 'post' command

/* --- GET LAST AUTO-INCREMENT ID INSERED AFTER POST COMMAND --- */
$firebase->getLastIdStored();

Go to next step!


Supported Commands

/* --- STORING DATA --- */
$firebase->post('path', $test,$options);
/* --- OVERRIDE DATA --- */
$firebase->put('path', $test,$options);
/* --- UPDATE DATA --- */
$firebase->patch('path', $test,$options);
/* --- RETRIEVE DATA --- */
$firebase->get('path',$options);
/* --- DELETE DATA --- */
$firebase->delete('path',$options);
/* --- RETRIEVE RULES --- */
$firebase->getRules('.settings/rules',$options);

Go to next step!


Manage rules via REST

####Read

require_once __DIR__ . '/vendor/autoload.php';
use ZendFirebase\Firebase;
use ZendFirebase\Authentication\FirebaseAuth;

$auth = new FirebaseAuth();

$auth->setBaseURI(/* complete with your firebase url */);
$auth->setServertoken(/* complete with your firebase token */);

$path = '.settings/rules'; // path of rules

$firebase = new Firebase($auth);


/* call function */
$firebase->getRules($path);

/* show rules! */
print_r($firebase->getFirebaseData());

####Write

require_once __DIR__ . '/vendor/autoload.php';
use ZendFirebase\Firebase;
use ZendFirebase\Authentication\FirebaseAuth;

$auth = new FirebaseAuth();

$auth->setBaseURI(/* complete with your firebase url */);
$auth->setServertoken(/* complete with your firebase token */);

$path = '.settings/rules'; // path of rules

$firebase = new Firebase($auth);

$rules =[
     "rules" => [
         ".read" => true,
         ".write" => "!data.exists() || !newData.exists()"
         ]
 ];
 
/* call function to write */
$firebase->writeRules($path,$rules);

/* show result! */
print_r($firebase->getFirebaseData());

now you are able to manage rules. Go to next step!


Rest Stream API

Create a new file your_file_name.php .

Inside this new file insert the following code :

use ZendFirebase\Firebase, ZendFirebase\Authentication\FirebaseAuth;

$auth = new FirebaseAuth();

$auth->setBaseURI('https://your_url_from_firebase/');
$auth->setServertoken('your_firebase_token');


/* --- CREATE NEW OBJECT AND PASS CREDENTIAL --- */
$firebase = new Firebase($auth);


$options = []; // container options as type array

$callback = 'callbackFunction'; // name of callback function as type string

function callbackFunction(...$params){
    // all code needed
}

$print = true;

/* --- SET PATH,
	   NAME OF FOLDER WHERE STORE LOGS,
	   MILLISECONDS OF DELAY BETWEEN NEW REQUEST (not required, default 5000),
	   CALLBACK FUNCTION,
	   ARRAY OPTIONS (not required, default []),
	   PRINT (not required, default TRUE) --- */
$firebase->startStream('path', 'logs/', 5000, $callback, $options, $print);

Now for run listener open terminal and run you file with command :

php your_file_name.php

This method start listener and write log file of changes.


PHPUnit Tests

All the unit tests are found in the "/tests" directory. Due to the usage of an interface, the tests must run in isolation.

Project Configuration it's just setted for doing all tests with the simple command :

cd <your_project>

phpunit

If you want to run a single test, just run :

cd <your_project>

phpunit name_and_path_of_the_file_that_you_want_to_test.php

BSD 3-Clause License

READ BSD LICENSE