phraseanet / php-sdk
用于与Phraseanet API交互的库
Requires
- php: ^7.2
- ext-json: *
- doctrine/cache: ~1.0
- doctrine/collections: ~1.0
- guzzlehttp/guzzle: ^7
Requires (Dev)
- phpunit/phpunit: ~5.5
- psr/log: ^1.1
- 7.x-dev
- 7.0.1
- 6.x-dev
- 6.0.1
- 6.0.0
- 1.x-dev
- 1.0.0
- 0.10.18
- 0.10.17
- 0.10.16
- 0.10.15
- 0.10.14
- 0.10.13
- 0.10.12
- 0.10.11
- 0.10.10
- 0.10.9
- 0.10.8
- 0.10.7
- 0.10.6
- 0.10.5
- 0.10.4
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.x-dev
- dev-master / 0.9.x-dev
- 0.9.9
- 0.9.8
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.x-dev
- 0.8.0
- 0.7.x-dev
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.2
- 0.1.0
- dev-PARADE-900-fix-facet-and-fields
- dev-PARADE-900-use-searchraw
- dev-PARADE-777/Bug_DataboxCollection_fromList_function
- dev-PHRAS-3225_bump-php-sdk-libs_MASTER
This package is not auto-updated.
Last update: 2024-09-19 15:48:38 UTC
README
Phraseanet PHP SDK 是一个用于与 Phraseanet API 交互的面向对象库。
安装
安装Phraseanet PHP SDK推荐通过composer进行。
composer require phraseanet/php-sdk:^0.9
基本使用
创建应用程序
创建Phraseanet SDK应用程序所需的最小配置;请注意,客户端client-id
、url
和secret
是必需的。请参阅在线文档以获取有关生成这些信息的更多信息。
$app = PhraseanetSDK\Application::create(array( 'client-id' => '409ee2762ff49ce936b2ca6e5413607a', 'secret' => 'f53ea9b0da92e45f9bbba67439654ac3', 'url' => 'https://your.phraseanet-install.com/', // Phraseanet install URI ));
获取OAuth令牌
创建应用程序后,需要令牌来查询API。有两种方式
开发者令牌
开发者令牌可以从Phraseanet开发者应用面板(我的账户 > 开发者 > 应用程序)中获取。
OAuth2认证流程
Phraseanet SDK提供了一个方便的方式来检索OAuth令牌。使用OAuth2Connector来完成
- 将最终用户重定向到Phraseanet授权URL
// Must be identical to the redirect URI set in your Oauth application configuration in Phraseanet. $redirectUri = 'http://myhost.dev/oauth-callback-endpoint/'; $connector = $app->getOauth2Connector(); $url = $connector->getAuthorizationUrl($redirectUri);
注意,可以通过传递给getAuthorizationUrl
方法的额外参数。请参阅有关可用参数的在线文档。
- 在您的应用程序回调中检索访问令牌
$connector = $app->getOauth2Connector(); $token = $connector->retrieveAccessToken($code, $redirectUri);
获取令牌后,您可以使用EntityManager
。
使用EntityManager
EntityManager
是检索Phraseanet实体的人口点。
$em = $app->getEntityManager($token); $query = $em->getRepository('Record')->search(array( 'query' => 'animals', 'offset_start' => 0, 'per_page' => 20, 'bases' => array(1, 4), 'record_type' => 'image' )); echo $query->getTotalResults() . " items found in " . $query->getQueryTime() . " seconds\n"; foreach($query->getResults() as $record) { echo "Record " . $record->getTitle() . "\n". foreach ($record->getSubdefs() as $subdef) { echo "subdef ". $subdef->getName() ." has URL " . $subdef->getPermalink()->getUrl() . "\n"; } }
将文件上传到Phraseanet
可以使用通过Application
对象公开的uploader实例将文件上传到Phraseanet
$uploader = $app->getUploader($token); $result = $uploader->upload('/path/to/file.jpg', $base_id); if ($result instanceof PhraseanetSDK\Entity\Record) { // record has been created } elseif ($result instanceof PhraseanetSDK\Entity\Quarantine) { // record has been quarantined }
$base_id
可以是base_id
值或PhraseanetSDK\Entity\DataboxCollection
实体。
请注意,您可以通过第三个参数强制行为,并将Phraseanet记录状态(二进制字符串)作为第四个参数传递
$result = $loader->upload('/path/to/file.jpg', $base_id, $behavior, '1011000');
行为可以是
- 0 强制记录
- 1 强制隔离
- null 让Phraseanet检查(默认行为)
配置
扩展API响应格式
Phraseanet API(v1.4.1)可以为记录对象提供扩展响应格式。
在这种情况下,所有与记录对象相关的信息(永久链接、子定义、标题、状态)都包含在响应中。
结果是,使用此功能,您只需要一个请求即可填充整个记录对象,而不是五个。
填充记录对象的时间略长,但与通过HTTP协议获取关系数据所花费的时间相比,可以忽略不计。
$app = PhraseanetSDK\Application::create(array( 'client-id' => '409ee2762ff49ce936b2ca6e5413607a', 'secret' => 'f53ea9b0da92e45f9bbba67439654ac3', 'url' => 'https://your.phraseanet-install.com/' )); $token = '899ee278736b2an6bs786e541ajk8'; // activate globally $app->getAdapter()->setExtended(true); // activate for current entity manager $em = $app->getEntityManager($token); $em->getAdapter()->setExtended(true);
日志
可以通过在配置中设置PSR Logger来记录请求以供监控或调试。请参阅https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
有关日志插件配置,请参阅http://guzzle3.readthedocs.org/plugins/log-plugin.html
use Psr\Log\LoggerInterface; class QueryLogger extends LoggerInterface { ... }
use PhraseanetSDK\Application; use Guzzle\Log\PsrLogAdapter; use Guzzle\Plugin\Log\LogPlugin; $client = Application::create(array( 'client-id' => '409ee2762ff49ce936b2ca6e5413607a', 'secret' => 'f53ea9b0da92e45f9bbba67439654ac3', 'url' => 'https://your.phraseanet-install.com/' ), array(new LogPlugin(new PsrLogAdapter(new QueryLogger())));
缓存
为了性能,强烈建议使用缓存系统。这可以通过以下配置轻松完成。
有关缓存插件配置,请参阅http://guzzle3.readthedocs.org/plugins/cache-plugin.html
use PhraseanetSDK\Application; use Doctrine\Common\Cache\FilesystemCache; use Guzzle\Cache\DoctrineCacheAdapter; use Guzzle\Plugin\Cache\CachePlugin; use Guzzle\Plugin\Cache\DefaultCacheStorage; $cachePlugin = new CachePlugin(array( 'storage' => new DefaultCacheStorage( new DoctrineCacheAdapter( new FilesystemCache('/path/to/cache/files') ) ) )); $client = Application::create( array( 'client-id' => '409ee2762ff49ce936b2ca6e5413607a', 'secret' => 'f53ea9b0da92e45f9bbba67439654ac3', 'url' => 'https://your.phraseanet-install.com/', ), array($cachePlugin));
Silex Provider
本包内包含了一个Silex提供者。
基本用法
$app = new Silex\Application(); $app->register(new PhraseanetSDK\PhraseanetSDKServiceProvider(), array( // required 'sdk.config' => array( 'client-id' => $clientId, // Your client id 'secret' => $secret, // You client secret 'url' => $url, // The ur of the phraseanet instance where you have created your application ), // optional 'cache.config' => array( 'type' => 'array', // can be 'array', 'memcache' or 'memcached'. Default value is 'array'. // options for memcache(d) cache type 'options' => array( 'host' => '127.0.0.1', 'port' => '11211' ) 'ttl' => '3600', // cache TTL in seconds. Default value is '3600'. 'revalidation' => null, // cache re-validation strategy can be null, 'skip' or 'deny' or an object that implements 'Guzzle\Plugin\Cache\RevalidationInterface' // Default value is null. // skip : never performs cache re-validation and just assumes the request is still ok // deny : never performs cache re-validation and just assumes the request is invalid // The default strategy if null is provided is to follow HTTP RFC. see https://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-26 // and https://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-26 'can_cache' => null, // can cache strategy can be null or an object that implements 'Guzzle\Plugin\Cache\CanCacheStrategyInterface' 'key_provider' => null, // key provider strategy can be null or an object that implements 'Guzzle\Plugin\Cache\CacheKeyProviderInterface' ), 'recorder.enabled' => false, // Enabled recorder 'recorder.config' => array( 'type' => 'file', // specified type of storage can be 'file', 'memcache' or 'memcached'. Default value is file 'options' => array( 'file' => '/path/to/file', // specified path to the file to write data, if specified type is file 'host' => '127.0.0.1', // specified host to the memcache(d) server , if specified type is memcache or memcached 'port' => '33', // specified port to the memcache(d) server, if specified type is memcache or memcached ), 'limit' => 1000, // specified limit of request to store ) ));
录制/播放请求
录制器
可以通过服务提供商使用以下代码在每个请求中启用录制器
$app = new Silex\Application(); $app->register(new PhraseanetSDK\PhraseanetSDKServiceProvider(), array( 'sdk.config' => array( 'client-id' => $clientId, 'secret' => $secret, 'url' => $url, ), 'recorder.enabled' => true, 'recorder.config' => array( 'type' => 'memcached', 'options' => array( 'host' => 'localhost', 'port' => 11211, ), 'limit' => 5000 // record up to 5000 requests ), ));
请求可以存储在memcache
、memcached
或file
中。要使用文件,配置应如下所示
$app = new Silex\Application(); $app->register(new PhraseanetSDK\PhraseanetSDKServiceProvider(), array( 'recorder.config' => array( 'type' => 'memcached', 'options' => array( 'host' => '127.0.0.1', 'port' => '/path/to/file', ), 'limit' => 5000 // record up to 5000 requests ), ));
播放器
要回放已存储的请求,请使用播放器
$player = $app['phraseanet-sdk.player.factory']($token); $player->play();
请注意,为了在不使用缓存的情况下播放请求(例如预热缓存),您必须使用deny
缓存重新验证策略
$app->register(new PhraseanetSDK\PhraseanetSDKServiceProvider(), array( 'sdk.config' => array( 'client-id' => $clientId, 'secret' => $secret, 'url' => $url, ), 'cache.config' = array( 'revalidation' => 'deny', // important ) ));
监控
SDK提供了一个监控Phraseanet的工具
$monitor = $app->getMonitor($token); $scheduler = $monitor->getScheduler(); echo sprintf("Scheduler state is %s", $scheduler->getState());
许可证
Phraseanet SDK是在MIT许可证下发布的。