PHP code example of antcool / easy-lark
1. Go to this page and download the library: Download antcool/easy-lark library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
antcool / easy-lark example snippets
$config = [
'debug' => env('APP_DEBUG', false),
'runtime_path' => storage_path('lark'),
'app_id' => '',
'app_secret' => '',
'http' => [
'timeout' => 10,
'base_uri' => 'https://open.feishu.cn',
],
'event' => [
'encrypt_key' => '',
'verify_token' => '',
'verify_request' => true,
],
'access_token' => \AntCool\EasyLark\Support\AccessToken::class,
];
// AppServiceProvider
public function boot()
{
$this->app->singleton('lark', fn () => new Application($config));
}
$app = app('lark');
try {
// 发起 API 请求
$response = $app->getClient()->getJson('uri', $query = []);
$response = $app->getClient()->postJson('uri', $data = [], $query = []);
// 免登授权
$response = $app->getClient()->postJson('/open-apis/authen/v1/access_token', [
'grant_type' => 'authorization_code',
'code' => 'dDieky8JXDywpnOlhR8ydf',
]);
// 飞书审批 (飞书部分接口使用的 www.feishu.cn 域名, 但是 AccessToken 相同)
$app->getClient()->postJson('https://www.feishu.cn/approval/openapi/v2/approval/get', [
'approval_code' => '376DA07B-XXXX-XXXX-XXXX-98B7B907C6B3',
]);
// 文件上传
$file = new \AntCool\EasyLark\Support\File('file path');
$app->getClient()->uploadFile(
'/open-apis/im/v1/files' // 消息与群组消息文件上传,
$file,
['file_type' => $file->extension, 'file_name' => $file->name]
);
} catch (Throwable $exception) {
echo $exception->getMessage();
}
// 订阅指定审批单
$response = app('lark')->getClient()->postJson(
'https://www.feishu.cn/approval/openapi/v2/subscription/subscribe',
['approval_code' => 'approval_code']
);
// 订阅事件处理
$server = $this->app->getServer();
// 方式一: 获取来自飞书服务器的推送事件内容, 你可以自行处理后 return $server->serve()
$event = $server->getRequestEvent();
// 方式二: 事件处理中间件处理, 可注册多个事件处理中间件
$server->with(function (Event $event, \Closure $next) {
$body = $event->getBody(); // 推送的消息内容
return $next($event);
});
// 方式三: 指定事件名称处理中间件
$server->addEventListener('approval_instance', function (Event $event, \Closure $next) {
$body = $event->getBody(); // 推送的消息内容
return $next($event);
});
// 别忘了调用 $server->serve();
return $server->serve();
class YourAccessToken implements AccessTokenInterface{
public function getToken():string
{
}
}
$config = [
'access_token' => YourAccessToken::class
];