adammbalogh/box-sdk

非官方的 Box.com Php Sdk

0.5.0 2014-10-20 18:22 UTC

This package is auto-updated.

Last update: 2024-08-29 01:54:11 UTC


README

Author Build Status Quality Score Software License Packagist Version Total Downloads

SensioLabsInsight

描述

这是一个非官方的 Box Php Sdk。

目录

支持

Support with Gittip

安装

通过 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();

请求

扩展请求

在这里,你可以看到一个对 View 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)获取 Entry 对象。

<?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