refill_proxy.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. class refill_proxy
  3. {
  4. private $mKey;
  5. public function __construct($key)
  6. {
  7. $this->mKey = $key;
  8. }
  9. public function send($url,$params)
  10. {
  11. $body = $this->body($params);
  12. $body .= "&key={$this->mKey}";
  13. $params['sign'] = md5($body);
  14. $resp = $this->http_request($url,$params,'POST');
  15. Log::record("resp={$resp}",Log::DEBUG);
  16. if(empty($resp)) {
  17. return [false,'网络错误'];
  18. }
  19. $resp = json_decode($resp,true);
  20. $code = $resp['code'];
  21. if(empty($resp)) {
  22. return [false,'网络错误'];
  23. }
  24. elseif($code == 200) {
  25. return [$code,'success'];
  26. }
  27. else {
  28. return [$code,$resp['message']];
  29. }
  30. }
  31. public function notify($params)
  32. {
  33. unset($params['mchname']);
  34. if(!$this->verify($this->mKey,$params)) {
  35. return [false,'签名错误'];
  36. }
  37. else {
  38. return [true,$params];
  39. }
  40. }
  41. private function verify($key,$input)
  42. {
  43. $sign = $input['sign'];
  44. unset($input['sign']);
  45. $body = $this->body($input);
  46. $body .= "&key={$key}";
  47. return ($sign == md5($body));
  48. }
  49. protected function check_empty($value)
  50. {
  51. if (!isset($value))
  52. return true;
  53. if ($value === null)
  54. return true;
  55. if (trim($value) === "")
  56. return true;
  57. return false;
  58. }
  59. private function body($params)
  60. {
  61. ksort($params);
  62. $body = "";
  63. $i = 0;
  64. foreach ($params as $k => $v)
  65. {
  66. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  67. {
  68. if ($i == 0) {
  69. $body .= "{$k}" . "=" . urlencode($v);
  70. } else {
  71. $body .= "&" . "{$k}" . "=" . urlencode($v);
  72. }
  73. $i++;
  74. }
  75. }
  76. return $body;
  77. }
  78. private function http_request($url, $params = array(), $method = 'GET', $multi = false, $extheaders = array())
  79. {
  80. if (!function_exists('curl_init')) {
  81. return false;
  82. }
  83. $method = strtoupper($method);
  84. $ci = curl_init();
  85. curl_setopt($ci, CURLOPT_USERAGENT, 'PHP-SDK OAuth2.0');
  86. curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
  87. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  88. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false);
  89. curl_setopt($ci, CURLOPT_HEADER, false);
  90. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60);
  91. curl_setopt($ci, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  92. curl_setopt($ci, CURLOPT_FOLLOWLOCATION, true);
  93. $headers = (array) $extheaders;
  94. switch ($method)
  95. {
  96. case 'POST':
  97. curl_setopt($ci, CURLOPT_POST, TRUE);
  98. if (!empty($params))
  99. {
  100. if ($multi)
  101. {
  102. foreach ($multi as $key => $file) {
  103. $params[$key] = '@' . $file;
  104. }
  105. curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
  106. $headers[] = 'Expect: ';
  107. }
  108. else {
  109. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  110. }
  111. }
  112. break;
  113. case 'DELETE':
  114. case 'GET':
  115. if($method == 'DELETE') {
  116. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  117. }
  118. if (!empty($params)) {
  119. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  120. }
  121. break;
  122. }
  123. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  124. curl_setopt($ci, CURLOPT_URL, $url);
  125. if ($headers) {
  126. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  127. }
  128. $response = curl_exec($ci);
  129. if($response == false) {
  130. $err = curl_error($ci);
  131. Log::record("http_request err={$err}",Log::ERR);
  132. }
  133. curl_close($ci);
  134. return $response;
  135. }
  136. }