crocodoc/php-box-view 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crocodoc/php-box-view

最新稳定版本:1.0.0

Composer 安装命令:

composer require crocodoc/php-box-view

包简介

PHP wrapper for the Box View API

README 文档

README

Introduction

php-box-view is a PHP wrapper for the Box View API. The Box View API lets you upload documents and then generate secure and customized viewing sessions for them. Our API is based on REST principles and generally returns JSON encoded responses, and in PHP are converted to associative arrays unless otherwise noted.

For more information about the Box View API, see the API docs at developers.box.com.

Installation

Requirements

  • PHP 5.4 or newer

Install with Composer

We recommend installing php-box-view using Composer.

If you don't have Composer, you can install it from the command line:

curl -sS https://getcomposer.org/installer | php

Use Composer to install the latest stable version:

composer require crocodoc/php-box-view

Make sure to add this package to your composer.json file. And if you aren't doing this already, require Composer's autoloader from your project:

require __DIR__ . '/path/to/root/vendor/autoload.php';

Install without Composer

Download the library and put it in your project. You will also need to download and include Guzzle.

From the file you want to include it from, just use the autoload file:

require __DIR__ . '/path/to/box/view/autoload.php';

Getting Started

Get an API Key

Create a Box Application to get an API key. Enter your application name, click the option for Box View, and click Create Application. Then click Configure your application.

You can find your API key where it says View API Key.

In the future, if you need to return to this page, go to Box Developers > My Applications and click on any of your Box View apps.

Examples

You can see a number of examples on how to use this library in examples/examples.php. These examples are interactive and you can run this file to see php-box-view in action.

To run these examples, open up examples/examples.php and change this line to show your API key:

$exampleApiKey = 'YOUR_API_KEY';

Save the file, make sure the examples/files directory is writable, and then run examples/examples.php:

php examples/examples.php

You should see 17 examples run with output in your terminal. You can inspect the examples/examples.php code to see each API call being used.

Your Code

To start using php-box-view in your code, set your API key:

$boxView = new Box\View\Client('YOUR_API_KEY');

Tests

First make sure you're running Composer and that you've run composer install.

Run the tests:

./vendor/bin/phpunit

Support

Please use GitHub's issue tracker for API library support.

Usage

Fields

All fields are accessed using getters. You can find a list of these fields below in their respective sections.

Errors

Errors are handled by throwing exceptions. We throw instances of Box\View\BoxViewException.

Note that any Box View API call can throw an exception. When making API calls, put them in a try/catch block. You can see examples/examples.php to see working code for each method using try/catch blocks.

Document

Fields

Field Getter
id $document->id()
createdAt $document->createdAt()
name $document->name()
status $document->status()

Upload from File

https://developers.box.com/view/#post-documents To upload a document from a local file, use $boxView->uploadFile(). Pass in a file resource, and also an optional associative array of other params. This function returns a Box\View\Document object.

// without options
$handle     = fopen($filename, 'r');
$document   = $boxView->uploadFile($handle);

// with options
$handle   = fopen($filename, 'r');
$document = $boxView->uploadFile($handle, [
    'name'       => 'Test File',
    'thumbnails' => ['100x100', '200x200'],
    'nonSvg'     => true,
]);

The response looks like this:

object(Box\View\Document)#54 (5) {
  ["createdAt":"Box\View\Document":private]=>
  string(25) "2015-03-11T07:48:52+00:00"
  ["id":"Box\View\Document":private]=>
  string(32) "0971e7674469406dba53254fcbb11d05"
  ["name":"Box\View\Document":private]=>
  string(11) "Sample File"
  ["status":"Box\View\Document":private]=>
  string(6) "queued"
}

Upload by URL

https://developers.box.com/view/#post-documents To upload a document by a URL, use $boxView->uploadUrl(). Pass in the URL of the file you want to upload, and also an optional associative array of other params. This function returns a Box\View\Document object.

// without options
$document = $boxView->uploadUrl($url);

// with options
$document = $boxView->uploadUrl($url, [
    'name'       => 'Test File',
    'thumbnails' => ['100x100', '200x200'],
    'nonSvg'     => true,
]);

The response looks like this:

object(Box\View\Document)#54 (5) {
  ["createdAt":"Box\View\Document":private]=>
  string(25) "2015-03-11T07:48:52+00:00"
  ["id":"Box\View\Document":private]=>
  string(32) "0971e7674469406dba53254fcbb11d05"
  ["name":"Box\View\Document":private]=>
  string(11) "Sample File"
  ["status":"Box\View\Document":private]=>
  string(6) "queued"
}

Get Document

https://developers.box.com/view/#get-documents-id To get a document, use $boxView->getDocument(). Pass in the ID of the document you want to get. This function returns a Box\View\Document object.

$document = $boxView->getDocument($documentId);

The response looks like this:

object(Box\View\Document)#54 (5) {
  ["createdAt":"Box\View\Document":private]=>
  string(25) "2015-03-11T07:48:52+00:00"
  ["id":"Box\View\Document":private]=>
  string(32) "0971e7674469406dba53254fcbb11d05"
  ["name":"Box\View\Document":private]=>
  string(11) "Sample File"
  ["status":"Box\View\Document":private]=>
  string(6) "queued"
}

Find

https://developers.box.com/view/#get-documents To get a list of documents you've uploaded, use $boxView->findDocuments(). Pass an optional associative array of parameters you want to filter by. This function returns an array of Box\View\Document objects matching the request.

// without options
$documents = $boxView->findDocuments();

// with options
$start     = date('c', strtotime('-2 weeks'));
$end       = date('c', strtotime('-1 week'));
$documents = $boxView->findDocuments([
    'limit'         => 10,
    'createdAfter'  => $start,
    'createdBefore' => $end,
]);

The response looks like this:

array(2) {
  [0]=>
  object(Box\View\Document)#31 (5) {
    ["createdAt":"Box\View\Document":private]=>
    string(25) "2015-03-11T07:50:41+00:00"
    ["id":"Box\View\Document":private]=>
    string(32) "f2f9be2249e2490da3b0a040d5eaae58"
    ["name":"Box\View\Document":private]=>
    string(14) "Sample File #2"
    ["status":"Box\View\Document":private]=>
    string(10) "processing"
  }
  [1]=>
  object(Box\View\Document)#55 (5) {
    ["createdAt":"Box\View\Document":private]=>
    string(25) "2015-03-11T07:50:40+00:00"
    ["id":"Box\View\Document":private]=>
    string(32) "966f747cb77b4f1b805cc594c9fdd30c"
    ["name":"Box\View\Document":private]=>
    string(11) "Sample File"
    ["status":"Box\View\Document":private]=>
    string(4) "done"
  }
}

Download

https://developers.box.com/view/#get-documents-id-content To download a document, use $document->download(). This function returns the contents of the downloaded file.

$contents = $document->download();
$filename = __DIR__ . '/files/new-file.doc';
$handle   = fopen($filename, 'w');

fwrite($handle, $contents);
fclose($handle);

The response is just a giant string representing the data of the file.

Thumbnail

https://developers.box.com/view/#get-documents-id-thumbnail To download a document, use $document->thumbnail(). Pass in the width and height in pixels of the thumbnail you want to download. This function returns the contents of the downloaded thumbnail.

$thumbnailContents = $document->thumbnail(100, 100);
$filename          = __DIR__ . '/files/new-thumbnail.png';
$handle            = fopen($filename, 'w');

fwrite($handle, $thumbnailContents);
fclose($handle);

The response is just a giant string representing the data of the file.

Update

https://developers.box.com/view/#put-documents-id To update the metadata of a document, use $document->update(). Pass in the fields you want to update. Right now, only the name field is supported. This function returns a boolean of whether the file was updated or not.

$updated = $document->update(['name' => 'Updated Name']);

if ($updated) {
    // do something
} else {
    // do something else
}

The response looks like this:

bool(true)

Delete

https://developers.box.com/view/#delete-documents-id To delete a document, use $document->delete(). This function returns a boolean of whether the file was deleted or not.

$deleted = $document->delete();

if ($deleted) {
    // do something
} else {
    // do something else
}

The response looks like this:

bool(true)

Session

Fields

Field Getter
id $session->id()
document $session->document()
expiresAt $session->expiresAt()
assetsUrl $session->assetsUrl()
realtimeUrl $session->realtimeUrl()
viewUrl $session->viewUrl()

Create

https://developers.box.com/view/#post-sessions To create a session, use $document->createSession(). Pass in an optional associative array of params. This function returns a Box\View\Session object.

// without options
$session = $document->createSession();

// with options
$session = $document->createSession([
    'expiresAt'        => date('c', strtotime('+10 min')),
    'isDownloadable'   => true,
    'isTextSelectable' => false,
]);

The response looks like this:

object(Box\View\Session)#41 (5) {
  ["document":"Box\View\Session":private]=>
  object(Box\View\Document)#27 (5) {
    ...
  }
  ["id":"Box\View\Session":private]=>
  string(32) "d1b8c35a69da43fbb2e978e99589114a"
  ["expiresAt":"Box\View\Session":private]=>
  string(25) "2015-03-11T08:53:23+00:00"
  ["urls":"Box\View\Session":private]=>
  array(3) {
    ["assets"]=>
    string(76) "https://view-api.box.com/1/sessions/d1b8c35a69da43fbb2e978e99589114a/assets/"
    ["realtime"]=>
    string(61) "https://view-api.box.com/sse/d1b8c35a69da43fbb2e978e99589114a"
    ["view"]=>
    string(73) "https://view-api.box.com/1/sessions/d1b8c35a69da43fbb2e978e99589114a/view"
  }
}

Delete

https://developers.box.com/view/#delete-sessions-id To delete a session, use $session->delete(). This function returns a boolean of whether the session was deleted or not.

$deleted = $session->delete();

if ($deleted) {
    // do something
} else {
    // do something else
}

The response looks like this:

bool(true)

crocodoc/php-box-view 适用场景与选型建议

crocodoc/php-box-view 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.57k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2015 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「crocodoc」 「box」 「box-view」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 crocodoc/php-box-view 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 crocodoc/php-box-view 我们能提供哪些服务?
定制开发 / 二次开发

基于 crocodoc/php-box-view 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 8
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: BSD
  • 更新时间: 2015-05-28