refill_proxy.php 4.0 KB

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