123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace outh;
- use mcryptdes\mcryptdes;
- class Outh
- {
- private $customer_code;
- private $api_url;
- private $verify_type = "0220";
- private $private_key;
- private $key;
- private $iv;
- /**
- * 构造函数
- * @param $sessionKey string 用户在小程序登录后获取的会话密钥
- * @param $appid string 小程序的appid
- */
- public function __construct($api_url,$customer_code,$private_key,$key)
- {
- $this->api_url = $api_url;
- $this->customer_code = $customer_code;
- $this->private_key = $private_key;
- $this->key = $key;
- $this->iv = '';
- }
- /**
- * 计算参数签名
- * $params 请求参数
- * $secretKey secretKey
- */
- function gen_signature($params)
- {
- $buff = "";
- foreach ($params as $key => $value) {
- if ($value !== null) {
- $buff .= strtoupper($key) . "=" . $value . "&";
- }
- }
- $buff = rtrim($buff, '&');
- return $this->genSign($buff,$this->private_key);
- }
- //生成 sha1WithRSA 签名
- function genSign($toSign, $privateKey){
- $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" .
- wordwrap($privateKey, 64, "\n", true) .
- "\n-----END RSA PRIVATE KEY-----";
- $key = openssl_get_privatekey($privateKey);
- openssl_sign($toSign, $signature, $key);
- openssl_free_key($key);
- $sign = base64_encode($signature);
- return $sign;
- }
- /**
- * 将输入数据的编码统一转换成utf8
- * @params 输入的参数
- */
- function toUtf8($params)
- {
- $utf8s = array();
- foreach ($params as $key => $value) {
- $utf8s[$key] = is_string($value) ? mb_convert_encoding($value, "utf8", "auto") : $value;
- }
- // 3DES 加密
- $desObj = new mcryptdes($this->key,$this->iv);
- $utf8s = json_encode($utf8s);
- $utf8s = $desObj->opensslEncrypt3DES($utf8s);
- return $utf8s;
- }
- /**
- * $params 请求参数
- */
- function toVerify($out_trade_no,$name,$cert_no) {
- if(!$out_trade_no || !$name || !$cert_no) return false;
- // 构造请求参数
- $params["out_trade_no"] = $out_trade_no;
- $params["tran_time"] = date("Y-m-d H:i:s");
- $params["verify_type"] = $this->verify_type;
- $params["name"] = $name;
- $params["cert_no"] = $cert_no;
- $params["sign"] = $this->gen_signature($params);
- $params["customer_code"] = $this->customer_code;
- // random int
- $params = strtoupper(bin2hex(base64_decode($this->toUtf8($params))));
- $result = $this->http_post_json($this->api_url,$params);
- if ($result === FALSE) {
- return array("code" => 500, "msg" => "file_get_contents failed.");
- } else {
- return json_decode($result[1], true);
- }
- }
- /**
- * @param $url
- * @param $jsonStr
- * @return array
- */
- function http_post_json($url, $jsonStr)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($jsonStr)
- )
- );
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- return array($httpCode, $response);
- }
- }
|