outh.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace outh;
  3. use mcryptdes\mcryptdes;
  4. class Outh
  5. {
  6. private $customer_code;
  7. private $api_url;
  8. private $verify_type = "0220";
  9. private $private_key;
  10. private $key;
  11. private $iv;
  12. /**
  13. * 构造函数
  14. * @param $sessionKey string 用户在小程序登录后获取的会话密钥
  15. * @param $appid string 小程序的appid
  16. */
  17. public function __construct($api_url,$customer_code,$private_key,$key)
  18. {
  19. $this->api_url = $api_url;
  20. $this->customer_code = $customer_code;
  21. $this->private_key = $private_key;
  22. $this->key = $key;
  23. $this->iv = '';
  24. }
  25. /**
  26. * 计算参数签名
  27. * $params 请求参数
  28. * $secretKey secretKey
  29. */
  30. function gen_signature($params)
  31. {
  32. $buff = "";
  33. foreach ($params as $key => $value) {
  34. if ($value !== null) {
  35. $buff .= strtoupper($key) . "=" . $value . "&";
  36. }
  37. }
  38. $buff = rtrim($buff, '&');
  39. return $this->genSign($buff,$this->private_key);
  40. }
  41. //生成 sha1WithRSA 签名
  42. function genSign($toSign, $privateKey){
  43. $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" .
  44. wordwrap($privateKey, 64, "\n", true) .
  45. "\n-----END RSA PRIVATE KEY-----";
  46. $key = openssl_get_privatekey($privateKey);
  47. openssl_sign($toSign, $signature, $key);
  48. openssl_free_key($key);
  49. $sign = base64_encode($signature);
  50. return $sign;
  51. }
  52. /**
  53. * 将输入数据的编码统一转换成utf8
  54. * @params 输入的参数
  55. */
  56. function toUtf8($params)
  57. {
  58. $utf8s = array();
  59. foreach ($params as $key => $value) {
  60. $utf8s[$key] = is_string($value) ? mb_convert_encoding($value, "utf8", "auto") : $value;
  61. }
  62. // 3DES 加密
  63. $desObj = new mcryptdes($this->key,$this->iv);
  64. $utf8s = json_encode($utf8s);
  65. $utf8s = $desObj->opensslEncrypt3DES($utf8s);
  66. return $utf8s;
  67. }
  68. /**
  69. * $params 请求参数
  70. */
  71. function toVerify($out_trade_no,$name,$cert_no) {
  72. if(!$out_trade_no || !$name || !$cert_no) return false;
  73. // 构造请求参数
  74. $params["out_trade_no"] = $out_trade_no;
  75. $params["tran_time"] = date("Y-m-d H:i:s");
  76. $params["verify_type"] = $this->verify_type;
  77. $params["name"] = $name;
  78. $params["cert_no"] = $cert_no;
  79. $params["sign"] = $this->gen_signature($params);
  80. $params["customer_code"] = $this->customer_code;
  81. // random int
  82. $params = strtoupper(bin2hex(base64_decode($this->toUtf8($params))));
  83. $result = $this->http_post_json($this->api_url,$params);
  84. if ($result === FALSE) {
  85. return array("code" => 500, "msg" => "file_get_contents failed.");
  86. } else {
  87. return json_decode($result[1], true);
  88. }
  89. }
  90. /**
  91. * @param $url
  92. * @param $jsonStr
  93. * @return array
  94. */
  95. function http_post_json($url, $jsonStr)
  96. {
  97. $ch = curl_init();
  98. curl_setopt($ch, CURLOPT_POST, 1);
  99. curl_setopt($ch, CURLOPT_URL, $url);
  100. curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
  101. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  102. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  103. 'Content-Type: application/json; charset=utf-8',
  104. 'Content-Length: ' . strlen($jsonStr)
  105. )
  106. );
  107. $response = curl_exec($ch);
  108. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  109. curl_close($ch);
  110. return array($httpCode, $response);
  111. }
  112. }