shiftplanning/humanity-php-sdk

Humanity PHP SDK

1.1.8 2016-05-08 11:01 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:21:32 UTC


README

安装

使用以下命令安装最新版本

$ composer require shiftplanning/humanity-php-sdk

用法

初始化

请确保加载 composer 自动加载文件。

<?php
use \Humanity\Entity\Company;
use \Humanity\Entity\Employee;
use \Humanity\Humanity;

// Load Humanity SDK for PHP via composer

$config = [
	'provider' => [
		'clientId' => '...',
		'clientSecret' => '...',
		'redirectUri' => '...',
		'scopes' => [
			Company::SCOPE_VIEW,
			Employee::SCOPE_VIEW
		],
	],
];

// Create new instance of humanity class
$humanity = new Humanity($config);

获取访问令牌

获取的访问令牌将被保存在 _SESSION 变量中。

// Obtain access token
$humanity->obtainAccessToken();
// Get access token instance
$accessToken = $humanity->getAccessToken();
printf('Access token: %s<br/>', $accessToken->accessToken);

检索登录员工数据

调用 Humanity::me() 将返回 Employee 实体实例

$me = $humanity->me();
printf('Hello, %s<br/>', $me->display_name);

与实体一起工作

检索数据

// Get Company repository instance
$companyRepository = $humanity->getCompanyRepository();
// Retrieve company data for current logged employee
$company = $companyRepository->get($me->company_id);
printf('Company: %s<br/>', $company->name);

// Get Employee repository instance
$employeeRepository = $humanity->getEmployeeRepository();
// Retrieve employees data for company.
$employees = $employeeRepository->getByCompany($company->company_id);

echo 'Employees: ';
echo '<ul>';
// Iterating employees collection
foreach ($employees as $employee) {
	printf('<li>%s</li>', $employee->display_name);
}
echo '</ul>';