yireo/producteev-client

此软件包已被放弃,不再维护。未建议替代软件包。

Producteev 的 PHP 客户端

0.0.1 2016-12-16 07:04 UTC

This package is auto-updated.

Last update: 2022-10-21 21:07:38 UTC


README

PHP 客户端,用于 Producteev API。

此 PHP 客户端使用 Producteev API,通过 OAuth2 进行身份验证,并依赖于 Guzzle。

状态

测试版(即“可能已准备好投入生产”)。此 API 客户端由我们自己用于将各种其他系统与 Producteev 集成,例如通过批处理脚本来创建完整的项目(包括任务和子任务)。

此软件包也列在 Packagist 上:[https://packagist.org.cn/packages/yireo/producteev-client](https://packagist.org.cn/packages/yireo/producteev-client)

安装

此客户端通过 composer 安装

composer config repositories.yireo-producteev vcs git@github.com:yireo/producteev-client.git
composer require yireo/producteev-client:dev-master

使用方法

首先,查看 Producteev 关于 API 使用的文档:[https://www.producteev.com/api/doc/#Introduction](https://www.producteev.com/api/doc/#Introduction)

阅读他们关于如何创建新应用的说明。记录下此应用的客户端 ID 和客户端密钥。在下面的示例中,这些是 $clientId$clientSecret 变量。

现在,设置一个网页,添加下面的代码,并访问它。在示例中,URL 将是 http://example.com

// Include all composer packages
require __DIR__ . '/vendor/autoload.php';

// Initialize the client with its ID and secret
$client = new \Yireo\ProducteevClient\Client($clientId, $clientSecret);

// Retrieve a current access token from cookie, if there
$client->retrieveAccessTokenFromCookie();

// Set the redirect URL
$redirectUrl = 'http://example.com/';
$client->setRedirectUrl($redirectUrl);

// When authenticating via OAuth2, Producteev will redirect back to us with a "code" set
if (!empty($_REQUEST['code'])) {
    $client->setAuthenticationCode($_REQUEST['code']);
    $client->retrieveAccessTokenToCookie();

    // Redirect to our same page again, removing the "code" part
    header('Location: ' . $redirectUrl);
    exit;
}

// When there is no valid access token, this will initiate OAuth2 authentication including a redirect to the Producteev webpage
if($client->isAccessTokenValid() === false) {
    $client->authenticate();
}

// Example call
echo '<pre>';
print_r($client->getResource('users')->me());
echo '</pre>';

Cookie

访问令牌存储在此代码示例中的浏览器 Cookie 中。如果您喜欢,可以使用其他存储方式。

API 调用

目前已提供以下调用

显示所有可用网络

$networks = $client->getResource('networks')->items();

获取当前用户

$me = $client->getResource('users')->me();

搜索特定用户

$exampleUser = $client->getResource('users')->findByEmail('info@example.com');

获取当前用户的默认项目

$defaultProject = $client->getResource('users')->getMyDefaultProject();

创建项目

$projectData = ['title' => 'Test Project', 'description' => 'Test description'];
$projectId = $client->getResource('projects')->create($projectData);

为现有项目创建任务,设置截止日期为明天,添加一些子任务,将任务分配给 $exampleUser 并从任务中移除当前用户

$taskDeadline = date('c', strtotime('tomorrow'));
$taskSubtasks = [['title' => 'Some subtask', 'status' => 1], ['title' => 'Another subtask', 'status' => 1]];
$taskData = ['title' => 'Test task', 'deadline' => $taskDeadline, 'subtasks' => $taskSubtasks, 'responsibles' => [$exampleUser], 'project' => ['id' => $projectId]];
$taskId = $client->getResource('tasks')->create($taskData);
$client->getResource('tasks')->removeResponsible($taskId, $me);

列出所有项目

$projects = $client->getResource('projects')->items();

搜索标题中包含“办公室”一词的项目

$projects = $client->getResource('projects')->search('office);