freekrai / simperium-php
这是一个用于与 Simperium API 在 PHP 中交互的非官方库。
该软件包的规范存储库似乎已不存在,因此该软件包已被冻结。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-01-20 11:08:34 UTC
README
这是一个用于与 Simperium API 在 PHP 中交互的非官方库。它基于 Simperium Python 库。
Simperium 是一种简单的方式,让开发者能够随着数据的变化而移动数据,即时且自动。这是一个 PHP 库。
您可以 注册 Simperium 的托管版本。还有其他语言的 Simperium 库。
这还不是完整的 Simperium 库,用于解析差异和更改。这是一个包装他们的 HTTP API,用于脚本编写和基本后端开发。
许可
Simperium PHP 库在 MIT 许可下免费和商业使用。
入门
要开始,请先登录到 https://simperium.com 并创建一个新的应用程序。记下新应用的名称、API 密钥和管理密钥。
在您的项目中安装 composer
curl -s https://getcomposer.org/installer | php
在项目根目录中创建一个 composer.json 文件
{
"require": {
"freekrai/simperium-php": "dev-master"
}
}
现在,运行 composer 并安装软件包
php composer.phar install
将此行添加到应用程序的 index.php 文件中
<?php
require 'vendor/autoload.php';
如果您没有使用 Composer 运行此操作,则将此行添加到 index.php 文件中
require_once dirname(__FILE__) . './Simperium/Simperium.php';
我们需要创建一个用户来存储数据
>>> $simperium = new Simperium\Simperium($yourappname,$yourapikey);
>>> $token = $simperium->create('joe@example.com', 'secret');
>>> echo $token
'25c11ad089dd4c18b84f24bc18c58fe2'
现在我们可以从 simperium 存储和检索数据。数据存储在桶中。例如,我们可以在 todo 桶中存储待办事项列表。当您存储项时,需要给它们一个唯一的标识符。通常 Uuid 是一个好的选择。
>>> $todo1_id = $simperium->generate_uuid();
>>> $simperium->todo->post($todo1_id,array('text' => 'Read general theory of love', 'done' => False));
我们可以检索此项目
>>> $simperium->todo->get($todo1_id);
{'text': 'Read general theory of love', 'done': False}
存储另一个待办事项
>>> $simperium->todo->post($simperium->generate_uuid(), array('text' => 'Watch battle royale', 'done'=> False) );
您还可以检索所有桶项的索引
>>> $simperium->todo->index();
{
'count': 2,
'index': [
{'id': 'f6b680f8504c4e31a0e54a95401ffca0', 'v': 1},
{'id': 'c0d07bb7c46e48e693653425eca93af9', 'v': 1}],
'current': '4f8507b8faf44720dfc432b1',}
检索索引中的所有文档
>>> foreach( $simperium->todo2->index()->index as $v ){
>>> echo $v->id.'<br />';
>>> $ret = $simperium->get( $v->id );
>>> }
[
{'text': 'Read general theory of love', 'done': False},
{'text': 'Watch battle royale', 'done': False}]
还可以通过 data=true 获取索引中每个文档的数据
>>> $simperium->todo->index(true)
{
'count': 2,
'index': [
{'id': 'f6b680f8504c4e31a0e54a95401ffca0', 'v': 1,
'd': {'text': 'Read general theory of love', 'done': False},},
{'id': 'c0d07bb7c46e48e693653425eca93af9', 'v': 1,
'd': {'text': 'Watch battle royale', 'done': False},}],
'current': '4f8507b8faf44720dfc432b1'}
要更新项中的字段,请发送更新的字段。它们将与当前文档合并
>>> $simperium->todo->post($todo1_id, array('done' => 'True'));
>>> $simperium->todo->get($todo1_id)
{'text': 'Read general theory of love', 'done': True}
Simperium 项是版本化的。您可以回退到过去并检索文档的旧版本
>>> $simperium->todo->get($todo1_id, $version=1)
{'text': 'Read general theory of love', 'done': False}
当然,您可以删除项
>>> $simperium->todo->delete($todo1_id)
>>> echo $simperium->todo->index()->count
1