在Laravel中引入钉钉SDK

最近在用Laravel开发钉钉企业内部应用,需要调用钉钉的SDK。不得不说,钉钉开发文档写的真是感人,开发的时候那是相当刺激。在使用SDK的时候遇到不少坑,钉钉的文档写的不是很详细,记录下在Laravel中集成DingTalk SDK的方法:

  1. 首先在http://open-doc.dingtalk.com/microapp/serverapi2/vzzrkv中下载PHP版本SDK。解压后文件目录如图
    这里我只用到dingtalk中的文件,删除aliyun、QimenCloud目录,保留top目录Autoloader.php和TopSdk.php文件。
  2. 在composer.json中:
        "autoload": {
            "classmap": [
                "database"
            ],
            "files":[
                "app/Packages/taobao/TopSdk.php"
            ],
            "psr-4": {
                "App\\": "app/"
            }
    
        },
    

    然后执行composer dump-autoload

  3. 执行完毕后,在Controller中就可以直接使用DingTalk SDK了。测试结果如下(注意要加‘ \ ’):
            $c = new \DingTalkClient(\DingTalkConstant::$CALL_TYPE_OAPI, \DingTalkConstant::$METHOD_POST , \DingTalkConstant::$FORMAT_JSON);
            var_dump($c);
    

    打印出结果如下:

    object(DingTalkClient)#276 (9) { ["gatewayUrl"]=> string(34) "http://eco.taobao.com/router/rest" ["format"]=> string(4) "json" ["connectTimeout"]=> NULL ["readTimeout"]=> NULL ["apiCallType"]=> string(4) "oapi" ["httpMethod"]=> string(4) "POST" ["checkRequest"]=> bool(true) ["apiVersion":protected]=> string(3) "2.0" ["sdkVersion":protected]=> string(25) "dingtalk-sdk-php-20161214" }
    

遇到的坑:

  1. 在调用DingTalkClient的execute方法获取token的时候,报错如下:
    Missing argument 7 for DingTalkClient::_executeOapi(), called in /Web/proj/app/Packages/taobao/dingtalk/DingTalkClient.php on line 328 and defined
    

    查看DingTalkClient.php文件,328行

        public function execute($request, $session = null,$bestUrl = null){
            if(DingTalkConstant::$CALL_TYPE_OAPI == $this->apiCallType){
                return $this->_executeOapi($request, $session, $bestUrl, null, null, null); 
            }else{
                return $this->_execute($request, $session, $bestUrl);
            }
        }
    

    查看_executeOapi方法的代码:

    private function _executeOapi($request, $session = null,$bestUrl = null,$accessKey, $accessSecret, $suiteTicket, $corpId)
    

    此处发现_executeOapi方法需要传7个参数,而在execute中只传了6个参数。修改后的代码如下:

        public function execute($request, $session = null,$bestUrl = null){
            if(DingTalkConstant::$CALL_TYPE_OAPI == $this->apiCallType){
                return $this->_executeOapi($request, $session, $bestUrl, null ,null, null, null);
            }else{
                return $this->_execute($request, $session, $bestUrl);
            }
        }
    

发表评论

电子邮件地址不会被公开。 必填项已用*标注