mobService.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare (strict_types=1);
  3. namespace library\lib;
  4. // +----------------------------------------------------------------------
  5. // | [ WE CAN DO IT MORE SIMPLE 秒验API ]
  6. // +----------------------------------------------------------------------
  7. // | Copyright (c) 2018-2020 rights reserved.
  8. // +----------------------------------------------------------------------
  9. // | Author: TABLE ME
  10. // +----------------------------------------------------------------------
  11. // | Date: 2020-11-10 13:21
  12. // +----------------------------------------------------------------------
  13. class mobService {
  14. private $config ;
  15. private $host = "http://identify.verify.mob.com";
  16. private $res;
  17. public function __construct()
  18. {
  19. $this->config = config('mob');
  20. }
  21. public function getResult(){
  22. return $this->res;
  23. }
  24. /**
  25. * run 数据
  26. * @param $token 服务器token
  27. * @param $opToken 运营商token
  28. * @param $operator 运营商类型,[CMCC:中国移动,CUCC:中国联通,CTCC:中国电信]
  29. */
  30. public function run($token,$opToken,$operator){
  31. //初始化
  32. $curl = curl_init();
  33. //设置抓取的url
  34. curl_setopt($curl, CURLOPT_URL, $this->host."/auth/auth/sdkClientFreeLogin");
  35. //设置头文件的信息作为数据流输出
  36. curl_setopt($curl, CURLOPT_HEADER, 0);
  37. //设置获取的信息以文件流的形式返回,而不是直接输出。
  38. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  39. //设置post方式提交
  40. curl_setopt($curl, CURLOPT_POST, 0);
  41. //设置post数据
  42. $post_data = array(
  43. "appkey" => $this->config['appkey'],
  44. "token" => $token,
  45. "opToken" => $opToken,
  46. 'operator'=> $operator,
  47. 'timestamp'=> $this->getUnixTimestamp()
  48. );
  49. if ($this->config['md5'] != '') {
  50. $post_data['md5'] = $this->config['md5'];
  51. }
  52. $post_data['sign'] = $this->getSign($post_data, $this->config['appSecret']);
  53. $jsonStr = json_encode($post_data);
  54. curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonStr);
  55. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  56. 'Content-Type: application/json; charset=utf-8',
  57. 'Content-Length: ' . strlen($jsonStr)
  58. )
  59. );
  60. //执行命令
  61. $data = curl_exec($curl);
  62. //关闭URL请求
  63. curl_close($curl);
  64. $json = json_decode($data);
  65. if ($json->status == 200) {
  66. $d = openssl_decrypt($json->res, "DES-CBC", $this->config['appSecret'], 0, "00000000");
  67. $this->res = json_decode($d, true);
  68. }
  69. }
  70. function getSign($data, $secret) {
  71. ksort($data);
  72. $str = '';
  73. foreach ($data as $k => $v ) {
  74. $str .= "$k=$v&";
  75. }
  76. $str = substr($str, 0, -1);
  77. return md5($str.$secret);
  78. }
  79. function getUnixTimestamp ()
  80. {
  81. list($s1, $s2) = explode(' ', microtime());
  82. return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
  83. }
  84. }