Coder Social home page Coder Social logo

weworkapi_php's Introduction

About

weworkapi_php 是为了简化开发者对企业微信API接口的使用而设计的,API调用库系列之php版本
包括企业API接口、消息回调处理方法、第三方开放接口等  
本库仅做示范用,并不保证完全无bug;
另外,作者会不定期更新本库,但不保证与官方API接口文档同步,因此一切以官方文档为准。

更多来自个人开发者的其它语言的库推荐:
python : https://github.com/sbzhu/weworkapi_python [email protected](企业微信团队)
ruby : https://github.com/mycolorway/wework MyColorway(个人开发者)
php : https://github.com/sbzhu/weworkapi_php [email protected](企业微信团队)
golang : https://github.com/sbzhu/weworkapi_golang [email protected](企业微信团队)
golang : https://github.com/doubliekill/EnterpriseWechatSDK [email protected](个人开发者)

Requirement

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

Director

├── api // API 接口
│   ├── datastructure // API接口需要使用到的一些数据结构
│   ├── examples // API接口的测试用例
│   ├── README.md
│   └── src // API接口的关键逻辑
├── callback // 消息回调的一些方法
├── config.php
├── README.md
└── utils // 一些基础方法

Usage

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

include_once("api/src/CorpAPI.class.php");

// 实例化 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 = "[email protected]";
        $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

[email protected]
[email protected]

weworkapi_php's People

Contributors

daidi avatar fenzaikai avatar sbzhu avatar wei4126 avatar xiaoyouxi01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

weworkapi_php's Issues

api\datastructure\Tag.class.php开头有个空行导致出错!

今天在测试JSSDK的时候,发现返回值res的length长度总是多了1个字符。

后来检查了大半天发现是api\datastructure\Tag.class.php文件开头有一个空行!!!

这个文件被CorpAPI.class.php包含,CorpAPI.class.php又被我的测试文档包含,结果……导致了出错。

我在删除这个空行之后,测试就正常了。

还请修正一下,删除这个空行~

EncryptMsg方法返回结果不正确

回复明文:
<xml><ToUserName><![CDATA[隐去]]></ToUserName><FromUserName><![CDATA[隐去]]></FromUserName><CreateTime>1542773288</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[欢迎使用本应用,本应用处于开发完善阶段,您可以留言提出想法建议!]]></Content><MsgId>1234567890123456</MsgId><AgentID>1000028</AgentID></xml>
EncryptMsg返回值:0
<xml> <Encrypt><![CDATA[���(��ǧ!#��V���]]></Encrypt> <MsgSignature><![CDATA[20c7c304ff8347ef4e2f04c772b0b1fc67f73d6e]]></MsgSignature> <TimeStamp>1542773532</TimeStamp> <Nonce><![CDATA[1542988481]]></Nonce> </xml>

callback文件夹中一些类的构造函数建议改为__construct

我在PHP7上测试服务器验证,一直不成功。
后来发现,是由于构造函数的问题,返回了一些PHP报错信息,导致验证不成功。
callback文件夹中,一些类的构造函数采用跟类名相同的命名方式,而不是用__construct。

例如下面这个public function WXBizMsgCrypt($token, $encodingAesKey, $Corpid)

`class WXBizMsgCrypt
{
private $m_sToken;
private $m_sEncodingAesKey;
private $m_sCorpid;

/**
 * 构造函数
 * @param $token string 公众平台上,开发者设置的token
 * @param $encodingAesKey string 公众平台上,开发者设置的EncodingAESKey
 * @param $Corpid string 公众平台的Corpid
 */
public function WXBizMsgCrypt($token, $encodingAesKey, $Corpid)
{
	$this->m_sToken = $token;
	$this->m_sEncodingAesKey = $encodingAesKey;
	$this->m_sCorpid = $Corpid;
}

}`

建议修改下~

这个官方是否已经放弃维护了?

如果是请在官网文档里注明一下。发现很多新功能都没有实现。比如 日历、日程等。
如果有其他的实现麻烦告知一下。不然就打算自己fork自己搞了

Message类中count()函数报错,还是null和array()的问题,希望能改一下~

之前修正过类似问题,php7.2中的count($args)函数,$args参数需为数组或者Countable对象,否则会报错。
#13

在weworkapi_php-master\api\datastructure\Message.class.php第9、10、11行:

    public $touser = null; // string array
    public $toparty = null; // uint array
    public $totag = null; // uint array 

这三个参数初始值建议改为array(),而不用null。
因为在第18、19、20行会对这三个参数使用count()函数

        if (count($this->touser) > 1000) throw new QyApiError("touser should be no more than 1000");
        if (count($this->toparty) > 100) throw new QyApiError("toparty should be no more than 100");
        if (count($this->totag) > 100) throw new QyApiError("toparty should be no more than 100");

在实例化Message类后,若这三个参数中的某个参数未赋值时,count(null)会导致报错。

同文件中第280、359行也有类似问题,可以一起改下。
public $articles = null;

关于JS-SDK:jsapi_ticket的缓存跟token的缓存一样吗?另外,可否增加生成签名?

在CorpAPI.class.php中看到获取jsapi_ticket的函数,请问下jsapi_ticket的缓存跟token的缓存一样吗?

/**
 * @brief JsApiTicketGet : 获取jsapi_ticket
 *
 * @link https://work.weixin.qq.com/api/doc#10029/获取jsapi_ticket
 *
 * @return : string ticket
 */
public function JsApiTicketGet()
{
    self::_HttpCall(self::GET_JSAPI_TICKET, 'GET', null); 
    return $this->rspJson["ticket"];
}

另外,可否在SDK中增加“获取自定义应用的签名对象”的代码?
api\examples\JsApiTest中只有获取jsapi_ticket,最好可以直接生成JSAPI签名,一步到位。

我在开发文档这个页面有看到这个函数function getSignPackage(),但是在SDK里没找到这个。。
https://work.weixin.qq.com/api/doc#10520/网页开发

php7.1.9 测试回调验证,总是报错。。= =!

用php7.1.9测试callback/Sample.php回调验证,总是报错。。。

第一次测试,结果:
①Notice: Use of undefined constant HttpUtils - assumed 'HttpUtils'
②Fatal error: Uncaught Error: Call to undefined function ParseUrl()

于是将HttpUtils.ParseUrl都修改成$_GET,进行第二次测试:
$sVerifyMsgSig = $_GET["msg_signature"];
$sVerifyTimeStamp = $_GET["timestamp"];
$sVerifyNonce = $_GET["nonce"];
$sVerifyEchoStr = $_GET["echostr"];
没了HttpUtils.ParseUrl就不报第一次测试的那个错误了,但是新的提示:
Function XXXX() is deprecated
这里XXXX()涉及好几个函数,都是提示deprecated
mcrypt_module_open
mcrypt_generic_init
mdecrypt_generic
mcrypt_generic_deinit
mcrypt_module_close
PHP官方文档显示:http://www.php.net/manual/en/function.mcrypt-module-open.php
This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged.
这些函数从php7.1.0开始已经不建议使用了。。。

最后无奈,由于我是用WampServer v3.1.0搭建的环境,所以将php版本切换到了7.0.23,这才成功。

上传临时素材,并以文件形式推送给用户,文件名变成了整个路径,如何解决?

用\api\examples\MediaTest.php测试,比如上传一个文件abc.docx。
上传后取得media_id,通过消息推送接口,将这个media_id以文件形式,通过某个自建应用发送给用户。
这时用户收到文件,显示的文件名是“D:\xxx\xxxx\xxxxx\xxxx\abc.docx”,文件名是整个路径。
能否修改下SDK,让上传的文件名还是原先的文件名?或者哪里可以设置下文件名吗?

api\datastructure\Message.class.php中FileMessageContent类的参数是不是多了?

这个文件消息的类

class FileMessageContent
{
    public $msgtype = "file"; 
	public $media_id = null; // string

	public function __construct($media_id=null, $title=null, $description=null)
	{
		$this->media_id = $media_id;
	}

构造函数中有3个参数($media_id、 $title、$description),其中$title、$description这两个参数是不是多余了?

开发文档中只有$media_id这个参数
https://work.weixin.qq.com/api/doc#10167/文件消息

PHP 7.0.x 弃用的功能:静态调用非静态的方法

根据官方文档:PHP 7.0.x 弃用的功能→静态调用非静态的方法
http://www.php.net/manual/zh/migration70.deprecated.php
01

在weworkapi_php-master\api\src\CorpAPI.class.php中,有多处静态调用Utils::Object2Array,会报错:

Deprecated: Non-static method Utils::Object2Array() should not be called statically in D:\Software\Wampserver\www\weworkapi_php-master\api\src\CorpAPI.class.php on line 438

Deprecated: Non-static method Utils::Object2Array() should not be called statically in D:\Software\Wampserver\www\weworkapi_php-master\utils\Utils.class.php on line 75

php7.2中,count()函数报错,请修正~

在php7.2中使用JSSDK时,遇到报错:
Warning: count(): Parameter must be an array or an object that implements Countable in C:\WampServer\www\weworkapi_php-master\api\src\API.class.php on line 97

    protected function _HttpCall($url, $method, $args)
    {
        if ('POST' == $method) { 
            $url = HttpUtils::MakeUrl($url);
            $this->_HttpPostParseToJson($url, $args);
            $this->_CheckErrCode();
        } else if ('GET' == $method) { 
            if (count($args) > 0) { 
                foreach ($args as $key => $value) {
                    if ($value == null) continue;
                    if (strpos($url, '?')) {
                        $url .= ('&'.$key.'='.$value);
                    } else { 
                        $url .= ('?'.$key.'='.$value);
                    }
                }
            }
            $url = HttpUtils::MakeUrl($url);
            $this->_HttpGetParseToJson($url);
            $this->_CheckErrCode();
        } else { 
            throw new QyApiError('wrong method');
        }
    }

经检查,发现是这里的count($args)报错,此处$args需为数组或者 Countable 对象。

官方文档中有说明:
http://php.net/manual/zh/function.count.php
版本 | 说明
7.2.0 | 当无效的 countable 类型传递给 array_or_countable 参数时,count() 会产生警告。

于是再回到调用 _HttpCall 函数的地方:
\weworkapi_php-master\api\src\CorpAPI.class.php line 792

    public function JsApiTicketGet()
    {
        self::_HttpCall(self::GET_JSAPI_TICKET, 'GET', null); 
        return $this->rspJson["ticket"];
    }

此处传入的参数是null,类型不符所以报错,改为空数组“[]”就可以了。

CorpAPI.class.php中还有其他地方也有调用_HttpCall 函数并且传入了null。
希望能针对这个问题做下调整,以兼容php7.2。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.