x3d/weworkapi

There is no license information available for the latest version (v1.0-alpha) of this package.

wework api with namespace

v1.0-alpha 2022-11-29 07:00 UTC

This package is not auto-updated.

Last update: 2024-05-15 12:44:41 UTC


README

weworkapi_php 是为了简化开发者对企业微信API接口的使用而设计的,API调用库系列之php版本,包括企业API接口、消息回调处理方法、第三方开放接口等。

原项目:https://github.com/sbzhu/weworkapi_php

本项目对原项目代码进行了梳理,加入了命名空间;并对测试用例加入了 PHPUnit 的引用。

不定期更新本库,但不保证与官方API接口文档同步,因此一切以官方文档为准。

Requirement

经测试,PHP 5.3.3 ~ 7.2.0 版本均可使用

Director

├── src // API 接口  
│ ├── struct // API接口需要使用到的一些数据结构  
│ ├── crypto // 消息加解密
│ ├── README.md  
├── demo // 消息回调的一些方法  
├── config.php   
├── README.md  
└── tests // 测试用例

Usage

将本项目下载到你的目录,可直接引用相关文件

use wework\CorpAPI;
use wework\struct\User;

// 实例化 API 类
$api = new CorpAPI($corpId='ww55ca070cb9b7eb22', $secret='ktmzrVIlUH0UW63zi7-JyzsgTL9NfwUhHde6or6zwQY');

try { 
    // 创建 User
    $user = new User();
    {
        $user->userid = "userid";
        $user->name = "name";
        $user->mobile = "131488888888";
        $user->email = "sbzhu@ipp.cas.cn";
        $user->department = array(1); 
    } 
    $api->UserCreate($user);

    // 获取User
    $user = $api->UserGet("userid");

    // 删除User
    $api->UserDelete("userid"); 
} catch {
    echo $e->getMessage() . "\n";
    $api->UserDelete("userid");
}

详细使用方法参考每个模块下的测试用例

关于token的缓存

token是需要缓存的,不能每次调用都去获取token,否则会中频率限制
在本库的设计里,token是以类里的一个变量缓存的
比如api/src/CorpAPI.class.php 里的$accessToken变量
在类的生命周期里,这个accessToken都是存在的, 当且仅当发现token过期,CorpAPI类会自动刷新token
刷新机制在 api/src/API.class.php
所以,使用时,只需要全局实例化一个CorpAPI类,不要析构它,就可一直用它调函数,不用关心 token

$api = new CorpAPI(corpid, corpsecret);
$api->dosomething()
$api->dosomething()
$api->dosomething()
....

当然,如果要更严格的做的话,建议自行修改,`全局缓存token,比如存redis、存文件等`,失效周期设置为2小时。

Contact us

web3d@live.cn