什么是认证码
认证码是接口访问的一个安全验证码
认证码生成规则:SHA256(应用ID + 应用密钥 + 时间戳)
接入概述
与WorkPro集成,开发者需要按照以下步骤完成:- 添加接入商
- 编写接口调用函数
- 根据接口文档实现业务逻辑
第一步:添加接入商,获取接入ID与密钥
第二步:编写接口调用函数(PHP代码示例)
/* * 声明 * 所有的结果返回格式是 status data err_code err_msg * status 0 错误 1 成功 * data 返回结果数据 * err_code 错误码 * err_msg 错误信息 * */ class IMSdk { //开发者需要这里---------------- private $appid = 'xxxx' ; private $appsecret = 'xxxxxxxxxxxxxxx' ; private $uid = '1' ; private $rest_site = 'http://localhost:8042' ; /** * 初始化 */ public function __construct(){ } /** * 请求接口 * @param unknown $module 模块名称 * @param unknown $method 方法 * @param unknown $param 参数 * @param unknown $timeout 超时时间 * @return {status:0/1,data:'',err_code:0,err_msg:''} */ public function getHttp($module,$method,$params = [],$timeout = 10){ //URL $url = $this->rest_site . "/api/$module/$method.html"; //HEADER $timestamp = time(); $headers = [ 'appid:'.$this->appid, 'appsecret:'.$this->appsecret, 'timestamp:'.$timestamp, 'authen:'.$this->getAuthen($timestamp), 'uid:'.$this->uid ]; //POSTFIELD $data = '' ; foreach($params as $key=>$value){ $data .= ($data == ""?"":"&") .$key . "=" . $value ; } //SUBMIT $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_POST, 1);//POST curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);//连接超时 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不返回状态 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//执行超时 $res = curl_exec($ch); curl_close($ch); return $res; } /** * 得到认证码 * @param unknown $timestamp * @return string */ private function getAuthen($timestamp){ $str = $this->appid . $this->appsecret . $timestamp ; return hash('sha256',$str); } }
第三步:根据接口文档实现业务逻辑
//引入Sdk文件 require 'IMSdk.class.php'; //实例化SDK $imsdk = new IMSdk(); $module = 'users'; $method = 'search'; $params = ['key'=>'1']; $res = $imsdk->getHttp($module,$method,$params); header("content-type:application/json;chartset=uft-8"); ob_clean(); echo($res); die();
示例代码下载:下载示例代码