# 示例代码

# PHP

  1. 通过composer安装

    composer require lexiangla/openapi:~v1.0
    
  2. 创建文件 index.php,使用创建文档接口为企业创建文档

<?php

include './vendor/autoload.php';

$suite_id = 'SUITE_ID';
$suite_secret = 'SUITE_SECRET';
$suite_ticket = 'SUITE_TICKET';

$company_id = 'COMPANY_ID';
$permanent_code = 'PERMANENT_CODE';

$staff_id = 'STAFF_ID';

$Lxservice = new \Lexiangla\Openapi\Service($suite_id, $suite_secret);

// 企业凭证 corp_access_token 需要缓存到服务器中,频繁获取会触发频率限制错误
if ($corp_access_token = cache_get('corp_access_token:'.$company_id)) {
    $Lxservice->setCorpAccessToken($corp_access_token);
} else {
    // 获取企业凭证需要先获取应用凭证,应用凭证同样需要缓存
    if ($suite_access_token = cache_get('suite_access_token')) {
        $Lxservice->setSuiteAccessToken($suite_access_token);
    } else {
        $Lxservice->setSuiteTicket($suite_ticket);
        cache_put('suite_access_token', $Lxservice->getSuiteToken(), 90); // 缓存90分钟
    }
    $corp_access_token = $Lxservice->getCorpAccessToken($company_id, $permanent_code);
    cache_put('corp_access_token'.$company_id, $corp_access_token, 90);
}

/** @var $corpClient \Lexiangla\Openapi\Api 企业开放接口SDK实例 */
$corpClient = $Lxservice->getCorpClient($company_id, $permanent_code);

$attributes = [
    'title' => '通过乐享api创建的文档标题',
    'content' => '<h1>欢迎使用腾讯乐享</h1>',
    'is_markdown' => 0,
];
$options = [
    'privilege_type' => 2,
    'category_id' => null,
    'team_id' => null,
    'directory_id' => null,
    'attachments' => [],
];
$response = $corpClient->postDoc($staff_id, $attributes, $options);
header('Content-Type: application/json');
echo json_encode($response);

/**
 * @param $key
 */
function cache_get($key)
{
    // TODO 需要开发者自行实现
}

/**
 * @param $key
 * @param $value
 * @param $minutes
 */
function cache_put($key, $value, $minutes)
{
    // TODO 需要开发者自行实现
}

使用上述代码示例,只要替换 $suite_id,$suite_secret,$suite_ticket,$company_id,$permanent_code,$staff_id 这些变量为开发者从腾讯乐享获取的值后即可调通。

cache_getcache_put 为服务端缓存的读写方法,需要开发者自行实现,否则会触发乐享接口服务的频率限制