CenterClient.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace extend\api;
  3. /**
  4. * 授权中心
  5. * 创建时间:2018年12月7日18:21:24
  6. * @author Administrator
  7. *
  8. */
  9. class CenterClient
  10. {
  11. // 请求地址
  12. private $request_url = "http://localhost/niucenter/?s=/niucenter/api/";
  13. // 平台标识
  14. private $name = "NIUCLOUD";
  15. private $app_key = "b61d312d32976df698ca34d098d23a9d";
  16. private $public_key;
  17. private $private_key;
  18. private $key_len;
  19. // 参数
  20. private $params = array();
  21. public function __construct()
  22. {
  23. $this->initRSA();
  24. $this->params['params'] = [
  25. 'name' => $this->name,
  26. 'app_key' => $this->app_key
  27. ];
  28. }
  29. /**
  30. * POST请求
  31. * 创建时间:2018年12月8日14:49:41
  32. * @param unknown $method 请求控制器/方法
  33. * @param unknown $params 参数
  34. * @return unknown
  35. */
  36. public function post($method, $params = [])
  37. {
  38. $this->request_url .= $method;
  39. $publicEncrypt = $this->publicEncrypt(json_encode($params));
  40. $this->params['params']['public_encrypt'] = $publicEncrypt;
  41. $res = HttpClient::post($this->request_url, $this->params);
  42. if (!empty($res)) {
  43. //对返回数据进行解密,解密需要一些时间,暂时去掉
  44. // $decrypted = $this->publicDecrypt($res);
  45. // if(!empty($decrypted)){
  46. // $res = json_decode($decrypted, true);
  47. // }else{
  48. // $res = json_decode($res, true);
  49. // }
  50. $res = json_decode($res, true);
  51. return $res;
  52. } else {
  53. return json_encode(error("接口发生错误"));
  54. }
  55. }
  56. /**
  57. * 初始化公钥 长度
  58. * 创建时间:2018年12月8日12:33:44
  59. *
  60. */
  61. private function initRSA()
  62. {
  63. $public_key = ADDON_APP_PATH . '/Niushop/rsa_public_key.pem';
  64. $public_key_content = file_get_contents($public_key);
  65. $this->public_key = openssl_pkey_get_public($public_key_content);
  66. $this->key_len = openssl_pkey_get_details($this->public_key)['bits'];
  67. }
  68. /*
  69. * 公钥加密
  70. */
  71. public function publicEncrypt($data)
  72. {
  73. $encrypted = '';
  74. $part_len = $this->key_len / 8 - 11;
  75. $parts = str_split($data, $part_len);
  76. //分段加密
  77. foreach ($parts as $part) {
  78. $encrypted_temp = '';
  79. openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
  80. $encrypted .= $encrypted_temp;
  81. }
  82. return $this->url_safe_base64_encode($encrypted);
  83. }
  84. /*
  85. * 公钥解密
  86. */
  87. public function publicDecrypt($encrypted)
  88. {
  89. $decrypted = "";
  90. $part_len = $this->key_len / 8;
  91. $base64_decoded = $this->url_safe_base64_decode($encrypted);
  92. $parts = str_split($base64_decoded, $part_len);
  93. foreach ($parts as $part) {
  94. $decrypted_temp = '';
  95. openssl_public_decrypt($part, $decrypted_temp, $this->public_key);
  96. $decrypted .= $decrypted_temp;
  97. }
  98. return $decrypted;
  99. }
  100. function url_safe_base64_encode($data)
  101. {
  102. return str_replace(array( '+', '/', '=' ), array( '-', '_', '' ), base64_encode($data));
  103. }
  104. function url_safe_base64_decode($data)
  105. {
  106. $base_64 = str_replace(array( '-', '_' ), array( '+', '/' ), $data);
  107. return base64_decode($base_64);
  108. }
  109. }