kjpay.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace kjpay;
  3. class Kjpay
  4. {
  5. /**
  6. * 获取IP地址
  7. * return ip
  8. */
  9. function get_wx_ip(){
  10. $ip = "unknown";
  11. if(isset($_SERVER['REMOTE_ADDR'])){
  12. $ip = $_SERVER['REMOTE_ADDR'];
  13. }elseif(getenv("REMOTE_ADDR")){
  14. $ip = getenv("REMOTE_ADDR");
  15. }elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
  16. $ip = $_SERVER['HTTP_CLIENT_IP'];
  17. } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  18. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  19. $pos = array_search('unknown', $arr);
  20. if (false !== $pos)
  21. unset($arr[$pos]);
  22. $ip = trim($arr[0]);
  23. }
  24. return $ip;
  25. }
  26. /**
  27. * 除去数组中的空值和签名参数
  28. * @param $para 签名参数组
  29. * return 去掉空值与签名参数后的新签名参数组
  30. */
  31. function paraFilters($para) {
  32. $para_filter = array();
  33. if($para){
  34. foreach ($para as $key=>$val){
  35. if($key == "sign" || $key == "signature" || $val == "")
  36. continue;
  37. else
  38. $para_filter[$key] = $para[$key];
  39. }
  40. }
  41. return $para_filter;
  42. }
  43. /**
  44. * 对数组排序
  45. * @param $para 排序前的数组
  46. * return 排序后的数组
  47. */
  48. function argSorts($para) {
  49. ksort($para);
  50. reset($para);
  51. return $para;
  52. }
  53. /**
  54. * 签名验证-快接支付
  55. * $datas 数据数组
  56. * $key 密钥
  57. */
  58. function local_sign($datas = array(), $key = ''){
  59. $str = urldecode(http_build_query($this->argSorts($this->paraFilters($datas))));
  60. $sign = md5($str."&key=".$key);
  61. return $sign;
  62. }
  63. /**
  64. * 签名验证-快接支付
  65. * $datas 数据数组
  66. * $key 密钥
  67. */
  68. function getdata($url, $param){
  69. $content = '';
  70. $ch = curl_init();
  71. curl_setopt($ch, CURLOPT_URL, $url);
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  73. curl_setopt($ch, CURLOPT_HEADER, false);
  74. curl_setopt($ch, CURLOPT_POST, true);
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  78. curl_setopt($ch, CURLOPT_TIMEOUT,6);
  79. $content = curl_exec($ch);
  80. //如果发现异常,排查下curl错误
  81. //echo "curl错误:".curl_error($ch);
  82. curl_close($ch);
  83. return $content;
  84. }
  85. }