PHP code example of lfphp / weworksdk
1. Go to this page and download the library: Download lfphp/weworksdk 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/ */
lfphp / weworksdk example snippets
use LFPhp\WeworkSdk\Service\Auth;
//由于授权部分逻辑复杂,该部分代码仅做演示。实际代码请参考官方API文档,
//调用Auth类下相应的方法。
//....
$access_token = Auth::getAccessToken($corp_id, $suite_access_token, $permanent_code);
//...继续正常逻辑
use LFPhp\WeworkSdk\Service\AddressBook\Department;
use LFPhp\WeworkSdk\Base\AuthorizedService;
use Xiaoe\Common\Exception\BusinessException;
//公共Token初始化,这部分代码可以放在公共用入口里面做。
AuthorizedService::setAccessToken($access_token);
//...more code
//查询部门列表
//非容错模式调用:
$dep_list = Department::getList();
var_dump($dep_list);
//容错模式调用:
try {
$dep_list = Department::getList();
} catch(BusinessException $be){
log('Error while get department list:'.$be->getMessage());
}
use LFPhp\WeworkSdk\ExternalAppEvent\Resolver;
use LFPhp\WeworkSdk\ExternalAppEvent\AddressBook\EventCreateUser;
//业务代码自行捕获事件回调参数
$xml_string = get_request();
/** @var EventAbstract $event **/
$event = Resolver::parser($xml_string);
switch(get_class($event)){
case EventCreateUser::class
//处理创建用户事件
var_dump($event->user_id);
break;
//case .... 更多事件类型处理
}
use LFPhp\Logger\Logger;
use LFPhp\Logger\LoggerLevel;
//...业务逻辑入口代码
$logger = Logger::instance(LFPhp\WeworkSdk\Base\Request::class);
$logger->register(new FileOutput('/tmp/wework.request.log'), LoggerLevel::INFO);