pimkaptein / box-sdk
此包已被弃用且不再维护。未建议替代包。
具有附加功能的 Box 内容/视图 SDK
0.6.0
2017-10-03 13:59 UTC
Requires
- php: >=5.4.0
- adammbalogh/key-value-store: ~0.5.1
- guzzlehttp/guzzle: ~5.3.0
Requires (Dev)
- phpmd/phpmd: ~2.1
- phpunit/phpunit: ~4.2.5
- squizlabs/php_codesniffer: ~1.5
This package is not auto-updated.
Last update: 2020-01-19 14:31:17 UTC
README
描述
这是一个非官方的 Box Php Sdk。
目录
支持
安装
通过 composer 安装。
{ "require": { "adammbalogh/box-sdk": "@stable" } }
提示:您应该浏览 adammbalogh/box-sdk
页面来选择要使用的稳定版本,避免使用 @stable
元约束。
授权
您的目标是获取一个有效的访问令牌。
授权
<?php use AdammBalogh\Box\Client\OAuthClient; use AdammBalogh\KeyValueStore\KeyValueStore; use AdammBalogh\KeyValueStore\Adapter\NullAdapter; use AdammBalogh\Box\Exception\ExitException; use AdammBalogh\Box\Exception\OAuthException; use GuzzleHttp\Exception\ClientException; $clientId = 'clientid'; $clientSecret = 'clientsecret'; $redirectUri = 'http://example.com/my-box-app.php'; $keyValueStore = new KeyValueStore(new NullAdapter()); $oAuthClient = new OAuthClient($keyValueStore, $clientId, $clientSecret, $redirectUri); try { $oAuthClient->authorize(); } catch (ExitException $e) { # Location header has set (box's authorize page) # Instead of an exit call it throws an ExitException exit; } catch (OAuthException $e) { # e.g. Invalid user credentials # e.g. The user denied access to your application } catch (ClientException $e) { # e.g. if $_GET['code'] is older than 30 sec } $accessToken = $oAuthClient->getAccessToken();
$keyValueStore
对象负责获取/保存访问令牌。上面的示例使用了一个 NullAdapter
作为 KeyValueStore
,这意味着它不会获取或保存任何内容,因此在每次调用上都进行授权。
如果您想持久保存访问(和刷新)令牌,您应该检查 KeyValueStore 包的其他适配器,在此。
撤销令牌
$oAuthClient->revokeTokens();
获取访问令牌 Ttl
/* @var int $ttl Access token's time to live */ $ttl = $oAuthClient->getAccessTokenTtl();
请求
扩展请求
在这里,您可以看到一个对视图 API 的示例请求。它调用 UrlDocumentUpload 命令。
许多命令能够包含一个扩展请求对象。通过扩展请求对象,您可以注入额外的头部、URL 参数或请求体属性。
<?php use AdammBalogh\Box\ViewClient; use AdammBalogh\Box\Client\View\ApiClient; use AdammBalogh\Box\Client\View\UploadClient; use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Request\ExtendedRequest; $apiKey = 'apikey'; $viewClient = new ViewClient(new ApiClient($apiKey), new UploadClient($apiKey)); $er = new ExtendedRequest(); $er->setHeader('Content-Type', 'application/json'); $er->addQueryField('fields', 'status'); $er->setPostBodyField('name', 'file-name'); $command = new View\Document\UrlDocumentUpload('https://cloud.box.com/shared/static/zzxlzc38hq7u1u5jdteu.pdf', $er);
响应
处理响应
您可以得到 5 个重要的响应值
- $response->getStatusCode(); 例如,'201'
- $response->getReasonPhrase(); 例如,'Created'
- $response->getHeaders(); 响应头数组 ['header name' => 'header value']
- $response->json(); 将 JSON 响应解析为数组
- (string)$response->getBody();
<?php use AdammBalogh\Box\ViewClient; use AdammBalogh\Box\Client\View\ApiClient; use AdammBalogh\Box\Client\View\UploadClient; use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $apiKey = 'apikey'; $viewClient = new ViewClient(new ApiClient($apiKey), new UploadClient($apiKey)); $command = new View\Document\ListDocument(); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { $response->getStatusCode(); $response->getReasonPhrase(); $response->getHeaders(); $response->json(); (string)$response->getBody(); } elseif ($response instanceof ErrorResponse) { # same as above }
内容 API
创建客户端
<?php use AdammBalogh\Box\ContentClient; use AdammBalogh\Box\Client\Content\ApiClient; use AdammBalogh\Box\Client\Content\UploadClient; $accessToken = 'accesstoken'; $contentClient = new ContentClient(new ApiClient($accessToken), new UploadClient($accessToken));
命令
用户命令
获取当前用户命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\User\GetCurrentUser(); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
文件夹命令
复制文件夹命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Folder\CopyFolder('sourceFolderId', 'destinationFolderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
创建文件夹命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Folder\CreateFolder('folderName', 'parentFolderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
创建共享文件夹链接命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; use AdammBalogh\Box\Request\ExtendedRequest; $er = new ExtendedRequest(); $er->setPostBodyField('shared_link', ['access'=>'open']); $command = new Content\Folder\CreateSharedFolderLink('folderId', $er); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
删除文件夹命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Folder\DeleteFolder('folderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
获取文件夹信息命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Folder\GetFolderInfo('folderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
列出文件夹命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Folder\ListFolder('folderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
列出文件夹协作命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Folder\ListFolderCollaborations('folderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
更新文件夹信息命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; use AdammBalogh\Box\Request\ExtendedRequest; $er = new ExtendedRequest(); $er->setPostBodyField('name', 'file-name'); $command = new Content\Folder\UpdateFolderInfo('folderId', $er); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
文件命令
复制文件命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\CopyFile('fileId', 'folderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
创建共享文件链接命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; use AdammBalogh\Box\Request\ExtendedRequest; $er = new ExtendedRequest(); $er->setPostBodyField('shared_link', ['access'=>'open']); $command = new Content\File\CreateSharedFileLink('fileId', $er); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
删除文件命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\DeleteFile('fileId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
下载文件命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\DownloadFile('fileId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
获取文件信息命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\GetFileInfo('fileId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
飞行前现有文件检查命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\PreFlightExistingFileCheck('fileId', fileSize); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
飞行前新文件检查命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\PreFlightNewFileCheck('fileName', fileSize, 'parentFolderId'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
更新文件信息命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; use AdammBalogh\Box\Request\ExtendedRequest; $er = new ExtendedRequest(); $er->setPostBodyField('name', 'file-name'); $er->setPostBodyField('description', 'file-description'); $command = new Content\File\UpdateFileInfo('fileId', $er); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
上传文件命令
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\UploadFile('fileName', 'parentFolderId', 'content'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
上传新文件版本命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\File\UploadNewFileVersion('fileId', 'content'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
搜索命令
搜索内容命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\Content; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new Content\Search\SearchContent('query'); $response = ResponseFactory::getResponse($contentClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
查看API
创建客户端
<?php use AdammBalogh\Box\ViewClient; use AdammBalogh\Box\Client\View\ApiClient; use AdammBalogh\Box\Client\View\UploadClient; $apiKey = 'apikey'; $viewClient = new ViewClient(new ApiClient($apiKey), new UploadClient($apiKey));
命令
文档命令
删除文档命令
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\DeleteDocument('documentId'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
获取文档内容命令 ☢
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\GetDocumentContent('documentId', 'zip'); # extension can be '', 'zip' or 'pdf' $response = $viewClient->request($command); echo (string)$response->getBody(); # the content of the document
获取文档信息命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\GetDocumentInfo('documentId'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
获取文档缩略图命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\GetDocumentThumbnail('documentId'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
列出文档命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\ListDocument(); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
多部分文档上传命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\MultipartDocumentUpload('content', 'filename.pdf'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
更新文档信息命令
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\UpdateDocumentInfo('documentId', 'newFileName'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
URL文档上传命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Document\UrlDocumentUpload('urlOfTheDocument'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
会话命令
创建文档会话命令
✔ 扩展请求
<?php use AdammBalogh\Box\Command\View; use AdammBalogh\Box\Factory\ResponseFactory; use AdammBalogh\Box\GuzzleHttp\Message\SuccessResponse; use AdammBalogh\Box\GuzzleHttp\Message\ErrorResponse; $command = new View\Session\CreateDocumentSession('documentId'); $response = ResponseFactory::getResponse($viewClient, $command); if ($response instanceof SuccessResponse) { # ... } elseif ($response instanceof ErrorResponse) { # ... }
包装器
搜索路径包装器
它包装了搜索内容命令,以便能够从一个路径字符串(如 /root/dir_1/dir_2,或 /pictures/img.png)中获取条目对象。
<?php use AdammBalogh\Box\Wrapper\SearchPath; use AdammBalogh\Box\Wrapper\Response\FolderEntry; use AdammBalogh\Box\Wrapper\Response\FileEntry; $wrapper = new SearchPath($contentClient); $entry = $wrapper->getEntry('/my-dir/example_dir'); if ($entry instanceof FolderEntry) { $entry->identity; # folderId # here you can create your own command, because now you have the folder id! } elseif ($entry instanceof FileEntry) { $entry->identity; }
创建文件夹包装器
它包装了创建文件夹命令,以便能够隐式地创建文件夹。
<?php use AdammBalogh\Box\Wrapper\CreateFolders; $wrapper = new CreateFolders($contentClient); $lastFolderId = $wrapper->create('/dir_1/dir_2/dir_3/dir_4'); # $lastFolderId means dir_4's id