php接入钉钉注册回调
作者:互联网
示例框架-Yii2.0+mysql+crm+crm管理系统
1:首先需要下载钉钉官方的提供的php加密Demo
2:相信在做注册审批回调的博友们已经将接入了钉钉sdk,我就不一一展示1了接入流程了—直接上发起代码示例(aeskey必须是43为切记)
3:官方开放平台提供的PHP Demo:pkcs7Encoder.php文件修改
function Prpcrypt($k){ $this->key = base64_decode($k . "=");}修改为构造函数形式:function __construct($k){ $this->key = base64_decode($k . "=");}12345678
4:官方开放平台提供的PHP Demo:DingtalkCrypt.php文件修改
//加密 public function encrypt($text, $corpid) { try { //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $corpid; // 网络字节序 // $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); // $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); //使用自定义的填充方式对明文进行补位填充 $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); // mcrypt_generic_init($module, $this->key, $iv); // //加密 // $encrypted = mcrypt_generic($module, $text); // mcrypt_generic_deinit($module); // mcrypt_module_close($module); //print(base64_encode($encrypted)); //使用BASE64对加密后的字符串进行编码 $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv ); return array(ErrorCode::$OK, base64_encode($encrypted)); } catch (Exception $e) { print $e; return array(ErrorCode::$EncryptAESError, null); } } //解密 public function decrypt($encrypted, $corpid) { try { $ciphertext_dec = base64_decode($encrypted); // $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); // mcrypt_generic_init($module, $this->key, $iv); // $decrypted = mdecrypt_generic($module, $ciphertext_dec); // mcrypt_generic_deinit($module); // mcrypt_module_close($module); $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); } try { //去除补位字符 $pkc_encoder = new PKCS7Encoder; $result = $pkc_encoder->decode($decrypted); //去除16位随机字符串,网络字节序和AppId if (strlen($result) < 16) return ""; $content = substr($result, 16, strlen($result)); $len_list = unpack("N", substr($content, 0, 4)); $xml_len = $len_list[1]; $xml_content = substr($content, 4, $xml_len); $from_corpid = substr($content, $xml_len + 4); } catch (Exception $e) { print $e; return array(ErrorCode::$DecryptAESError, null); } if ($from_corpid != $corpid) return array(ErrorCode::$ValidateSuiteKeyError, null); return array(0, $xml_content); }1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
6:测试回调url示例
public function actionReceiveCallBack(){ //接收值 sign 时间戳 $signature=$_GET['signature']; $nonce=$_GET['nonce']; $timeStamp=$_GET['timestamp']; $suiteKey=Yii::$app->params['corpid'];//必填,企业ID $token="dingtalk"; //必须和在注册是一样 //接收传过来的需要解密的值 $postdata = file_get_contents("php://input"); $postList = json_decode($postdata,true); $encrypt = $postList['encrypt']; //注册时加密的key $aesKey=\Yii::$app->params['aes_key']; $aes_key_encode=base64_encode($aesKey); $aes_key=substr($aes_key_encode,0,-1); $decryptMsg=""; $crypt = new DingCallbackCrypt($token,$aes_key,$suiteKey); $encryData = $crypt->DecryptMsg($signature,$timeStamp,$nonce,$encrypt,$decryptMsg); //解密 if($encryData['errcode']!==0){ }else{ if($newData->EventType=="bpms_task_change"||$newData->EventType=="bpms_instance_change"){ $dingtalk=new DingtalkController(); $dingtalk->UpdateProcess($newData->processInstanceId); } $msg="success"; $encryptMsg=""; $data = $crypt->EncryptMsg($msg,$timeStamp,$nonce,$encryptMsg); //加密 if($data['errcode']!=0){ }else{ return $data['data']; //返回加密成功的json数据 } } }1234567891011121314151617181920212223242526272829303132333435
我这里是直接将注册的事件写死了 ,需要注册其他的更改一下就可以。
标签:加密,注册,接入,text,base64,module,key,mcrypt,php 来源: https://blog.csdn.net/fd746/article/details/113832375