refill_proxy.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_IPRESOLVE, CURL_IPRESOLVE_V4);
  91. $headers = (array) $extheaders;
  92. switch ($method)
  93. {
  94. case 'POST':
  95. curl_setopt($ci, CURLOPT_POST, TRUE);
  96. if (!empty($params))
  97. {
  98. if ($multi)
  99. {
  100. foreach ($multi as $key => $file) {
  101. $params[$key] = '@' . $file;
  102. }
  103. curl_setopt($ci, CURLOPT_POSTFIELDS, $params);
  104. $headers[] = 'Expect: ';
  105. }
  106. else {
  107. curl_setopt($ci, CURLOPT_POSTFIELDS, http_build_query($params));
  108. }
  109. }
  110. break;
  111. case 'DELETE':
  112. case 'GET':
  113. if($method == 'DELETE') {
  114. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  115. }
  116. if (!empty($params)) {
  117. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($params) ? http_build_query($params) : $params);
  118. }
  119. break;
  120. }
  121. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  122. curl_setopt($ci, CURLOPT_URL, $url);
  123. if ($headers) {
  124. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  125. }
  126. $response = curl_exec($ci);
  127. if($response == false) {
  128. $err = curl_error($ci);
  129. Log::record("http_request err={$err}",Log::ERR);
  130. }
  131. curl_close($ci);
  132. return $response;
  133. }
  134. }