pdjshog/onedrive-php-sdk 问题修复 & 功能扩展

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

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

pdjshog/onedrive-php-sdk

Composer 安装命令:

composer require pdjshog/onedrive-php-sdk

包简介

OneDrive SDK for PHP. (forked/updated for guzzle 7.2)

README 文档

README

OneDrive SDK for PHP is an open source library that allows PHP applications to interact programmatically with the OneDrive REST API.

It supports operations such as creating, reading, updating, deleting (CRUD) files and folders, as well as moving or copying them to other folders.

Requirements

Using the OneDrive SDK for PHP requires the following:

  • PHP 7.3 or newer
  • Composer 1.0.0-alpha10 or newer
  • Basic PHP knowledge

Testing

For development, you also require:

  • A OneDrive web application configured with http://localhost:7777/ as its redirect URI
  • A WebDriver server, for example the Selenium's Java standalone server
  • A Chrome browser & ChromeDriver, and they must be usable by the WebDriver server

Installation

The recommended way to install OneDrive SDK for PHP is to install it using Composer:

composer require pdjshog/onedrive-php-sdk

If you are not already using Composer in your PHP project, refer to the Composer documentation to learn how to set it up first.

Quick start

When using OneDrive SDK for PHP, you are assumed to have an application exposing a URL receiving user authentication data from OneDrive. Typically, this URL, referred to as your application's Redirect URI, is a PHP script accepting this data to expose functionality interacting with your users' OneDrive contents. An example of such an application is our functional test suite.

You also need to register a OneDrive application. To do this, first sign in to Microsoft Azure, then visit App registrations and add a registration for your application. While registering your application, you are given the option to set its Redirect URI, explained above. We currently only support Web redirect URIs. Once created, your application is assigned an Application (client) ID, referred to as its Client ID, and in Certificate & secrets, you need to add at least one Client secret. Warning: Client Secrets are similar to passwords or private keys, they allow applications to identify as yours: they should be handled securely and not be disclosed to third-parties.

Once you have a Redirect URI, a Client ID, and a Client Secret, your application can start using the OneDrive SDK for PHP in three steps.

Step 1: create your configuration

As you may need them from several scripts, we recommend saving your Client ID, Client secret and Redirect URI in a configuration file, for example:

<?php
// config.php

return [
    /**
     * Your OneDrive client ID.
     */
    'ONEDRIVE_CLIENT_ID' => '<YOUR_CLIENT_ID>',

    /**
     * Your OneDrive client secret.
     */
    'ONEDRIVE_CLIENT_SECRET' => '<YOUR_CLIENT_SECRET>',

    /**
     * Your OneDrive redirect URI.
     */
    'ONEDRIVE_REDIRECT_URI' => 'http://your.domain.com/redirect.php',
];

Step 2: direct your users to the sign in page

This script is responsible for, given a set of privileges, fetching a login URL from the OneDrive API. It then needs to guide the users to this URL so they initiate their log in and privilege granting process. The script should look like this:

<?php
// index.php

($config = include __DIR__ . '/config.php') or die('Configuration file not found');
require_once __DIR__ . '/vendor/autoload.php';

use Krizalys\Onedrive\Onedrive;

// Instantiates a OneDrive client bound to your OneDrive application.
$client = Onedrive::client($config['ONEDRIVE_CLIENT_ID']);

// Gets a log in URL with sufficient privileges from the OneDrive API.
$url = $client->getLogInUrl([
    'files.read',
    'files.read.all',
    'files.readwrite',
    'files.readwrite.all',
    'offline_access',
], $config['ONEDRIVE_REDIRECT_URI']);

session_start();

// Persist the OneDrive client' state for next API requests.
$_SESSION['onedrive.client.state'] = $client->getState();

// Redirect the user to the log in URL.
header('HTTP/1.1 302 Found', true, 302);
header("Location: $url");

Step 3: get an OAuth access token

After the users follow this URL, they are required to sign in using a valid Microsoft account, and they are asked whether they agree to allow your application to access their OneDrive account.

If they do, they are redirected back to your Redirect URI and a code is passed in the query string of this URL. The script residing at this URL essentially:

  1. Instantiates a Client from your configuration and the state from previous instantiations
  2. Obtains an OAuth access token using Client::obtainAccessToken() passing it the code received
  3. May start interacting with the files and folders stored in their OneDrive account, or delegates this responsibility to other scripts instantiating a Client from the same state

It typically looks like (replace /path/to by the appropriate values):

<?php
// redirect.php

($config = include __DIR__ . '/config.php') or die('Configuration file not found');
require_once __DIR__ . '/vendor/autoload.php';

use Krizalys\Onedrive\Onedrive;

// If we don't have a code in the query string (meaning that the user did not
// log in successfully or did not grant privileges requested), we cannot proceed
// in obtaining an access token.
if (!array_key_exists('code', $_GET)) {
    throw new \Exception('code undefined in $_GET');
}

session_start();

// Attempt to load the OneDrive client' state persisted from the previous
// request.
if (!array_key_exists('onedrive.client.state', $_SESSION)) {
    throw new \Exception('onedrive.client.state undefined in $_SESSION');
}

$client = Onedrive::client(
    $config['ONEDRIVE_CLIENT_ID'],
    [
        // Restore the previous state while instantiating this client to proceed
        // in obtaining an access token.
        'state' => $_SESSION['onedrive.client.state'],
    ]
);

// Obtain the token using the code received by the OneDrive API.
$client->obtainAccessToken($config['ONEDRIVE_CLIENT_SECRET'], $_GET['code']);

// Persist the OneDrive client' state for next API requests.
$_SESSION['onedrive.client.state'] = $client->getState();

// Past this point, you can start using file/folder functions from the SDK, eg:
$file = $client->getRoot()->upload('hello.txt', 'Hello World!');
echo $file->download();
// => Hello World!

$file->delete();

For details about classes and methods available, see the API reference or the project page on Krizalys.

Versioning

OneDrive SDK for PHP adheres to semantic versioning: we are committed not to introduce breaking changes to the public API without incrementing the major version number. We also try to document such changes in the changelog.

However, we only consider symbols marked with the @api annotation to be part of the public API and subject to the Semantic Versioning requirements. Other symbols are internal to this library; they may change or get removed regardless of the major version number. You should not rely on these in your code.

Testing

To run the functional test suite:

  1. Set your application configuration at test/functional/config.php ;

  2. Run your WebDriver server, for example:

    java -jar selenium-server-standalone-3.141.59.jar
  3. Run the functional test suite (it assumes that WebDriver listening on port 4444):

    vendor/bin/phpunit --configuration test/functional
  4. Repeat step 3 as needed.

License

The OneDrive SDK for PHP is licensed under the 3-Clause BSD License.

Credits

The OneDrive SDK for PHP is developed and maintained by Christophe Vidal.

pdjshog/onedrive-php-sdk 适用场景与选型建议

pdjshog/onedrive-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 pdjshog/onedrive-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 69
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-12-09