Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PHP connection to firebase work?

Tags:

php

firebase

If you have the time to read the firebase-php documentation, you can see there how to use the helper library to "connect" to firebase. But unfortunately, I think that the connection could only be established by phpunit, in other words, output can only be seen in the terminal. Since when you run the php pages in your browser, it will return fatal errors. Does anyone know how to use the helper library to connect to the firebase data without using phpunit? Thank you in advance.

like image 827
Lyka San Pedro Hementera Avatar asked Feb 03 '15 06:02

Lyka San Pedro Hementera


1 Answers

Using the lib is very easy, when you just look at the source code of the tests.

There are two kinds of tests:

  • a real functionality test, which uses cURL requests to the server and therefore is slow during testing with PHPUnit
  • a mocked functionality test (stub), which simulates the connection to and the response from the server, which is faster during testing

Now, in order to use firebase-php, you would simply do the same things as in the real functionality test. Include the lib, prepare the connection object with login credentials and then call the method you want. The interface describes, which methods you can expect in the firebaseLib class - or just look at the lib source itself.

This piece of code should get you started:

require '/path/to/libs/firebase-php/firebaseLib.php';
$url = '...';
$token = '...';
$firebase = new Firebase($url, $token);
$firebase->get('something/from/somewhere');

If you fetch the library via composer, you might declare an autoloading classmap, too. The author hasn't done this, yet.

{
    "autoload": {
        "classmap": ["vendor/ktamas77/firebase-php/firebaseLib.php"]
    }
}

Then simply require Composer's Autoloader with require "vendor/autoload.php"; and new Firebase to autoload the class.


How to get the auth token

  • open your firebase console
  • go to project settings
  • go to the database
  • then copy the secret key

get-firebase-token

like image 113
Jens A. Koch Avatar answered Sep 21 '22 22:09

Jens A. Koch