Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

507
Visualizações
How to get data from Firebase database using PHP?

Recently, I created a Firebase project composed by two parts, one is an iOS app and the other is a website. Everything was fine until I discovered that I needed to use PHP to load content dynamically into the page instead of just JavaScript.

On further research I found out that I could not retrieve data from the Firebase database using JavaScript and then use this data on a PHP script.

Since there is no documentation for PHP on Firebase docs, I started looking for some answer and I found out a project on github called Firebase Admin SDK for PHP. So here I am after many frustrated tries to retrieve data from Firebase using PHP asking for help. Here is what I did:

  • Installed Composer with:

    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php
    php -r "unlink('composer-setup.php');"

  • CD into projects folder and run:

    php composer.phar require kreait/firebase-php

  • Create a new index.php on the project to test

  • Add to the top of index.php:

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

  • Inside Fibase project overview, create a service account and save the FriendlyChat-1f226af19083.json to the project folder.

  • Add to index.php:

    $firebase = (new \Firebase\Factory())
    ->withCredentials(DIR.'/FriendlyChat-1f226af19083.json')
    ->withDatabaseUri('https://friendlychat-82d4c.firebaseio.com/')
    ->create();

    $database = $firebase->getDatabase();
    $reference = $database->getReference('/k1');
    $snapshot = $reference->getSnapshot();
    $k1 = $snapshot->getValue();
    echo $k1.'';

Here is the project database: enter image description here

What I hoped was to see "teste" printed in the page. But the page simply renders blank, can anyone help me on this matter? I'm not sure where to go next, as soon as I'm able to get and set data from the database I can actually start developing the new website in php.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

$acc = ServiceAccount::fromJsonFile(__DIR__.'/key/xxxxxx.json');
            $firebase = (new Factory)->withServiceAccount($acc)->create();
            $this->database = $firebase->getDatabase();
$reference = $database->getReference('/K1');
$snapshot = $reference->getSnapshot()->getValue();

The correct way to connect with you Firebase DB

about 4 years ago · Santiago Trujillo Relatório

0

<?php 
require __DIR__.'/../vendor/autoload.php';
use Kreait\Firebase\Factory;
$factory = (new Factory)
        ->withServiceAccount('secret/serviceaccount.json')
        ->withDatabaseUri('link_to_your_firebase_database');


$database = $factory->createDatabase();
$reference = $database->getReference('Services/');

$snapshot = $reference->getSnapshot();
$value = $snapshot->getValue();
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda