LoginSignup
0
0

More than 5 years have passed since last update.

Etherpad-lite PHPのAPIモジュール

Last updated at Posted at 2018-05-07

Etherpad-lite PHPのAPIモジュール

これは何?

Etherpad-liteのHTTP APIをPHPのモジュールを使って操作する方法の紹介です。

Etherpad-lite PerlのAPIモジュール
https://qiita.com/tomonsaurus/items/cde571b278725eb6bc19
に続きそのPHP版です。

APIについてはさわりのみです。
他の人にも役立つ機会はそれほど多くなさそうですがご参考までに。

環境

CentOS 7.4
php 5.4.16

PHPのAPIモジュール

  • 本稿を書いている途中で気づきましたが、このAPIより、下の「もう一つのPHPのAPIモジュール」の方が使いやすそうです。そっちを先に見て下さい。

導入方法

composerでインストール


$ composer require 0x46616c6b/etherpad-lite-client

使い方の詳しい解説は見当たらず、ソースを少し読んで試しました。
PHPのバージョンが古いものを使ったので、入るモジュールのバージョンもGithubの最新(v0.2.2)よりも古いもの(v0.1.1)になりました。そういう前提でお読みください。


// 例
// v0.2.2だとこうできる。
// $authorID =$res->getData('authorID');

// v0.1.1だとgetDataに引数が指定できず少し面倒。
$data = $res->getData();
$authorID = $data['authorID'];

利用方法

test.php

<?php

require 'vendor/autoload.php';

$url='http://192.168.33.10:9001';
$apikey ='secret';


$client = new \EtherpadLite\Client($apikey, $url);
$res = $client->createAuthorIfNotExistsFor('100','Bob');
$data = $res->getData();
$authorID = $data['authorID'];

print "authorID: $authorID\n";


$res = $client->createGroupIfNotExistsFor('999'); 
$data = $res->getData();
$groupID = $data['groupID']; 
print "groupID: ".$groupID. "\n";

$res = $client->createGroupPad($groupID, "newpad123", "Default Text");
$data = $res->getData();
$padID = $data['padID'];
print "padID: $padID\n";


$validUntil = 1893423600; // unix time 2030/1/1 00:00:00
$res = $client->createSession($groupID, $authorID, $validUntil);
$data = $res->getData();
$sessionID = $data['sessionID'];

print "sessionID: $sessionID\n";
?>

実行するとこんな感じ

[vagrant@localhost ~]$ php test.php 
authorID: a.0tQAzUqOGy432zOf
groupID: g.qagLxGJbOnWc30Lh
padID: g.qagLxGJbOnWc30Lh$newpad123
sessionID: s.5d0201f2ec84e916b21be93877518396

取得したsessionIDの値をクッキーに書き込めば作成したドキュメントの編集権限が得られます。

もう一つのPHPのAPIモジュール

もう一つPHPのAPIがあってこっちの方が使いやすそうです。
https://github.com/tomnomnom/etherpad-lite-client

composer require tomnomnom/etherpad-lite-client
tom.php
<?php

require 'vendor/autoload.php';

$client = new EtherpadLite\Client('secret', 'http://192.168.33.10:9001/api');

$author = $client->createAuthorIfNotExistsFor('100', 'Tom');
print "authorID: ".$author->authorID;
print "\n";


$group = $client->createGroupIfNotExistsFor('333');
print "groupID: ". $group->groupID;
print "\n";

$pad = $client->createGroupPad($group->groupID, "Gone with the wind", "Default Text");
print "padID: ". $pad->padID;
print "\n";

$validUntil = 1893423600;

$session = $client->createSession($group->groupID, $author->authorID, $validUntil);

print "sessionID: ". $session->sessionID;
print "\n";
?>
[vagrant@localhost ~]$ php tom.php 
authorID: a.0tQAzUqOGy432zOf
groupID: g.5YpvcuKLW6N9r2xj
padID: g.5YpvcuKLW6N9r2xj$Gone_with_the_wind
sessionID: s.b2bbec166c28aef79e8590c19c4fdfde

同様に取得したsessionIDの値をクッキーに書き込めば作成したドキュメントの編集権限が得られます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0