refill_proxy.php 4.0 KB

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