eelkevdbos / firebase-php
Firebase php封装REST API
0.1.3
2015-05-07 21:51 UTC
Requires
- php: >=5.4
- firebase/php-jwt: ~2.0
- guzzlehttp/guzzle: 5.*
- illuminate/support: >=4.0
Requires (Dev)
- mockery/mockery: 0.9.1
- phpunit/phpunit: 4.1.1
This package is not auto-updated.
Last update: 2024-09-14 15:34:46 UTC
README
Firebase php封装REST API
##先决条件
- PHP >= 5.4
- Firebase >= 1.1.1
- Composer(推荐,但不是必需的)
使用Composer安装(推荐)
在composer.json
中将项目的最小稳定性设置为dev
。这是由于PHP-JWT依赖项引起的。更新composer.json文件后,只需执行:composer require eelkevdbos/firebase-php dev-master
##不使用Composer安装 For一个纯净的安装,以下依赖项需要下载
可以通过使用任何PSR-4自动加载器来加载依赖项。
基本用法
通过将您的Firebase密钥作为令牌,您将获得对Firebase的超级用户访问权限。
use Firebase\Firebase; $fb = Firebase::initialize(YOUR_FIREBASE_URL, YOUR_FIREBASE_SECRET); //or set your own implementation of the ClientInterface as second parameter of the regular constructor $fb = new Firebase([ 'base_url' => YOUR_FIREBASE_BASE_URL, 'token' => YOUR_FIREBASE_SECRET ], new GuzzleHttp\Client()); //retrieve a node $nodeGetContent = $fb->get('/node/path'); //set the content of a node $nodeSetContent = $fb->set('/node/path', array('data' => 'toset')); //update the content of a node $nodeUpdateContent = $fb->update('/node/path', array('data' => 'toupdate')); //delete a node $nodeDeleteContent = $fb->delete('/node/path'); //push a new item to a node $nodePushContent = $fb->push('/node/path', array('name' => 'item on list'));
高级用法
对于更精细的认证,请查看安全规则。使用令牌生成器允许您利用Firebase提供的认证服务。
use Firebase\Firebase; use Firebase\Auth\TokenGenerator; $tokenGenerator = new TokenGenerator(YOUR_FIREBASE_SECRET); $token = $tokenGenerator->generateToken(['email' => 'test@example.com']) $fb = Firebase::initialize(YOUR_FIREBASE_BASE_URL, $token);
上面的PHP代码片段与以下安全规则交互
{
"rules": {
".read": "auth.email == 'test@example.com'"
".write": "auth.email == 'admin@example.com'"
}
}
并且将允许代码片段对所有节点进行读取访问,但不能进行写入访问。
##并发请求 可以使用与常规请求相同的语法执行并发请求。只需将它们包裹在闭包中,并通过batch
方法调用闭包即可。
use Firebase\Firebase; $fb = Firebase::initialize(YOUR_FIREBASE_BASE_URL, YOUR_FIREBASE_SECRET); $requests = $fb->batch(function ($client) { for($i = 0; $i < 100; $i++) { $client->push('list', $i); } }); $pool = new GuzzleHttp\Pool($fb->getClient(), $requests); $pool->wait();
集成
在撰写本文时,支持Laravel 4.*的集成。提供了一个服务提供者和一个外观类。在常规安装步骤之后,通过两个简单的步骤进行安装
- 编辑
app/config/app.php
以添加服务提供者和外观类
'providers' => array(
...
'Firebase\Integration\Laravel\FirebaseServiceProvider'
)
'aliases' => array(
...
'Firebase' => 'Firebase\Integration\Laravel\Firebase'
)
- 编辑
app/config/services.php
(默认由L4.2提供)以添加token
和base_url
设置
'firebase' => array(
'base_url' => YOUR_FIREBASE_BASE_URL,
'token' => YOUR_FIREBASE_SECRET
)
##事件
该库支持EventEmitter模式。事件发射器附加到Firebase类。当前可用的事件
- RequestsBatchedEvent