[英]How to get child count of a parent from Firebase using PHP?
I'm developing an application like facebook to create posts and users can comment and like to the posts. 我正在开发一个类似facebook的应用程序来创建帖子,用户可以评论和喜欢这些帖子。 I use firebase realtime database to store likes and comments data for posts.
我使用firebase实时数据库存储帖子的点赞和评论数据。 Follow the image below,
跟随下图,
Now I need to get the posts to ascending order by comments count and likes count. 现在,我需要按评论数和喜欢数将帖子按升序排列。 How can I do this using PHP ?
如何使用PHP做到这一点?
You can find an unofficial Admin SDK for PHP at https://github.com/kreait/firebase-php that makes this fairly easy. 您可以在https://github.com/kreait/firebase-php上找到用于PHP的非官方Admin SDK,这使得此操作相当容易。
Once installed and set up , you can do this: 安装并设置好后 ,您可以执行以下操作:
<?php
require 'vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
$serviceAccount = ServiceAccount::fromJsonFile('/path/to/credentials.json');
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->create();
$db = $firebase->getDatabase();
echo count($db->getReference('path/to/comments')->shallow()->getValue());
I don't know a more comfortable way to do it in PHP 😇 (Disclaimer: I created the SDK 😅) 我不知道用PHP😇做的更舒适的方法(免责声明:我创建了SDK😅)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.