PHP code example of lascyb / feishu-sdk-php

1. Go to this page and download the library: Download lascyb/feishu-sdk-php 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/ */

    

lascyb / feishu-sdk-php example snippets


use feishu\Client;
use feishu\open_apis\im\v1\messages\MessagesCreateRequest;

$client = new Client($appId, $appSecret);

$response = $client->request(new MessagesCreateRequest(/* ... */));
$data = $response->data();

use feishu\open_apis\contact\v3\departments\children\ChildrenListRequest;
use feishu\open_apis\contact\v3\users\find_by_department\FindByDepartmentGetRequest;

// 获取子部门列表(根部门 department_id 为 "0")
$response = $client->request(new ChildrenListRequest('0', pageSize: 50));

// 获取部门直属用户列表
$response = $client->request(new FindByDepartmentGetRequest('od-xxx', pageSize: 50));

use feishu\AnyRequest;
use feishu\Client;

$client = new Client($appId, $appSecret);

// GET
$response = $client->request(AnyRequest::get('/contact/v3/users/' . $userId, [
    'user_id_type' => 'open_id',
]));

// POST
$response = $client->request(AnyRequest::post(
    '/im/v1/messages',
    [
        'receive_id' => $openId,
        'msg_type' => 'text',
        'content' => json_encode(['text' => 'hello'], JSON_UNESCAPED_UNICODE),
    ],
    ['receive_id_type' => 'open_id']
));

// 或直接构造:new AnyRequest('PATCH', '/path', $query, $body)

use feishu\WebhookBot;

$bot = new WebhookBot($webhookUrl, $secret); // $secret:飞书后台开启签名校验时传入
$bot->验

$bot->sendText('应用报警:服务异常');
$bot->sendText(WebhookBot::atUser('ou_xxx', 'Tom') . ' 请处理');
$bot->sendPost([
    'zh_cn' => [
        'title' => '项目更新',
        'content' => [[['tag' => 'text', 'text' => '项目有更新']]],
    ],
]);
$bot->sendImage('img_xxx');
$bot->sendShareChat('oc_xxx');
$bot->sendCard($card);

use feishu\OAuth2;

$oauth = new OAuth2($appId, $appSecret, $redirectUri);

// 跳转授权
$authUrl = $oauth->authorize(['auth:user.id:read'], $state);

// 回调换 token
$token = $oauth->getAccessToken($code);
// $token['access_token'] / $token['refresh_token'] ...

// 刷新 token
$token = $oauth->refreshAccessToken($refreshToken);
bash
composer