CaptchaClient.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: dingxiang-inc
  5. * Date: 2017/8/24
  6. * Time: 21:05
  7. */
  8. include_once __DIR__ . "/model/CaptchaResponse.php";
  9. class CaptchaClient
  10. {
  11. private $captchaUrl = "https://cap.dingxiang-inc.com/api/tokenVerify"; // 顶象验证码服务后台token验证URL
  12. private $appId;
  13. private $appSecret;
  14. private $captchaResponse;
  15. private $timeout = 2;
  16. function __construct($appId, $appSecret)
  17. {
  18. $this->appId = $appId;
  19. $this->appSecret = $appSecret;
  20. }
  21. public function setTimeOut($timeout)
  22. {
  23. if ($timeout < 0) {
  24. # code...
  25. return;
  26. }
  27. $this->timeout = $timeout;
  28. }
  29. public function setCaptchaUrl($captchaUrl)
  30. {
  31. $this->captchaUrl = $captchaUrl;
  32. }
  33. public function verifyToken($token)
  34. {
  35. $captchaResponse = new CaptchaResponse(false, "");
  36. if (is_null($this->appId) || is_null($this->appSecret) || is_null($token)) {
  37. $captchaResponse->setServerStatus("参数错误");
  38. return $captchaResponse;
  39. }
  40. $params = explode(":", $token);
  41. $constId = null;
  42. if (count($params) == 2) {
  43. $constId = $params[1];
  44. }
  45. $sign = md5($this->appSecret . $params[0] . $this->appSecret);
  46. $requestUrl = $this->captchaUrl . "?appKey=" . $this->appId . "&constId=" . $constId . "&token=" . $params[0] . "&sign=" . $sign;
  47. $httpResponse = $this->do_request($requestUrl, $this->timeout, $captchaResponse);
  48. return $captchaResponse;
  49. }
  50. private function do_request($requestUrl, $timeout, $captchaResponse)
  51. {
  52. $params = array('http' => array(
  53. 'method' => 'GET',
  54. 'header' => 'Content-type:text/html',
  55. 'timeout' => $timeout
  56. ));
  57. $ctx = stream_context_create($params);
  58. $fp = @fopen($requestUrl, 'r', false, $ctx);
  59. if (!$fp) {
  60. $this->setResponse($captchaResponse, "server connect failed!", $fp);
  61. return;
  62. }
  63. $response = @stream_get_contents($fp);
  64. if ($response === false) {
  65. $this->setResponse($captchaResponse, "get response failed!", $fp);
  66. $this->close($fp);
  67. return;
  68. }
  69. $obj = json_decode($response);
  70. if ($obj == null) {
  71. $this->setResponse($captchaResponse, "get response failed!", $fp);
  72. $this->close($fp);
  73. return;
  74. }
  75. $captchaResponse->setServerStatus("SERVER_SUCCESS");
  76. $captchaResponse->setResult($obj->success);
  77. $this->close($fp);
  78. }
  79. public function __set($property_name, $value)
  80. {
  81. $this->$property_name = $value;
  82. }
  83. public function setResponse($captchaResponse, $msg, $fp)
  84. {
  85. $captchaResponse->setResult(true);
  86. $captchaResponse->setServerStatus($msg);
  87. $this->close($fp);
  88. }
  89. public function close($fp)
  90. {
  91. try {
  92. if ($fp != null) {
  93. fclose($fp);
  94. }
  95. } catch (Exception $e) {
  96. echo "close error:" . $e->getMessage();
  97. }
  98. }
  99. }