title: How to get data from Firebase database using PHP?
body: <p>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.</p>
  
  <p>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. </p>
  
  <p>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 <a href="https://github.com/kreait/firebase-php" rel="noreferrer">Firebase Admin SDK for PHP</a>. So here I am after many frustrated tries to retrieve data from Firebase using PHP asking for help. Here is what I did:</p>
  
  <ul>
  <li><p>Installed Composer with:   </p>
  
  <p>php -r "copy('<a href="https://getcomposer.org/installer" rel="noreferrer">https://getcomposer.org/installer</a>', 'composer-setup.php');"<br>
  php composer-setup.php<br>
  php -r "unlink('composer-setup.php');"</p></li>
  <li><p>CD into projects folder and run:</p>
  
  <p>php composer.phar require kreait/firebase-php</p></li>
  <li><p>Create a new index.php on the project to test</p></li>
  <li><p>Add to the top of index.php:  </p>
  
  <p>require <strong>DIR</strong>.'/vendor/autoload.php';</p></li>
  <li><p>Inside Fibase project overview, create a service account and save the FriendlyChat-1f226af19083.json to the project folder.</p></li>
  <li><p>Add to index.php:</p>
  
  <p>$firebase = (new \Firebase\Factory())<br>
  ->withCredentials(<strong>DIR</strong>.'/FriendlyChat-1f226af19083.json')<br>
  ->withDatabaseUri('<a href="https://friendlychat-82d4c.firebaseio.com/" rel="noreferrer">https://friendlychat-82d4c.firebaseio.com/</a>')<br>
  ->create();</p>
  
  <p>$database = $firebase->getDatabase();<br>
  $reference = $database->getReference('/k1');<br>
  $snapshot = $reference->getSnapshot();<br>
  $k1 = $snapshot->getValue();<br>
  echo $k1.'';</p></li>
  </ul>
  
  <p>Here is the project database:
  <a href="https://i.stack.imgur.com/Zih3Z.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Zih3Z.png" alt="enter image description here"></a></p>
  
  <p>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.</p>