JuheRecharge.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\common\library;
  3. use app\common\model\Config;
  4. use think\Controller;
  5. // +----------------------------------------------------------------------
  6. // | JuhePHP [ NO ZUO NO DIE ]
  7. // +----------------------------------------------------------------------
  8. // | Copyright (c) 2010-2015 http://juhe.cn All rights reserved.
  9. // +----------------------------------------------------------------------
  10. // | Author: Juhedata <info@juhe.cn-->
  11. // +----------------------------------------------------------------------
  12. //----------------------------------
  13. // 聚合数据-手机话费充值API调用类
  14. //----------------------------------
  15. class JuheRecharge {
  16. private $appkey;
  17. private $openid;
  18. private $telCheckUrl = 'http://op.juhe.cn/ofpay/mobile/telcheck';
  19. private $telQueryUrl = 'http://op.juhe.cn/ofpay/mobile/telquery';
  20. private $submitUrl = 'http://op.juhe.cn/ofpay/mobile/onlineorder';
  21. private $staUrl = 'http://op.juhe.cn/ofpay/mobile/ordersta';
  22. public function __construct(){
  23. $this->appkey = Config::get_values('juhe_appkey');
  24. $this->openid = Config::get_values('juhe_openid');
  25. }
  26. /**
  27. * 根据手机号码及面额查询是否支持充值
  28. * @param string $mobile [手机号码]
  29. * @param int $pervalue [充值金额]
  30. * @return boolean
  31. */
  32. public function telcheck($mobile,$pervalue){
  33. $params = 'key='.$this->appkey.'&phoneno='.$mobile.'&cardnum='.$pervalue;
  34. $content = $this->juhecurl($this->telCheckUrl,$params);
  35. $result = $this->_returnArray($content);
  36. return $result;
  37. // print_r($result);die;
  38. // if($result['error_code'] == '0'){
  39. // return true;
  40. // }else{
  41. // return false;
  42. // }
  43. }
  44. /**
  45. * 根据手机号码和面额获取商品信息
  46. * @param string $mobile [手机号码]
  47. * @param int $pervalue [充值金额]
  48. * @return array
  49. */
  50. public function telquery($mobile,$pervalue){
  51. $params = 'key='.$this->appkey.'&phoneno='.$mobile.'&cardnum='.$pervalue;
  52. $content = $this->juhecurl($this->telQueryUrl,$params);
  53. return $this->_returnArray($content);
  54. }
  55. /**
  56. * 提交话费充值
  57. * @param [string] $mobile [手机号码]
  58. * @param [int] $pervalue [充值面额]
  59. * @param [string] $orderid [自定义单号]
  60. * @return [array]
  61. */
  62. public function telcz($mobile,$pervalue,$orderid){
  63. $pervalue = floatval($pervalue);
  64. $sign = md5($this->openid.$this->appkey.$mobile.$pervalue.$orderid);//校验值计算
  65. $params = array(
  66. 'key' => $this->appkey,
  67. 'phoneno' => $mobile,
  68. 'cardnum' => $pervalue,
  69. 'orderid' => $orderid,
  70. 'sign' => $sign
  71. );
  72. $content = $this->juhecurl($this->submitUrl,$params,1);
  73. return $this->_returnArray($content);
  74. }
  75. /**
  76. * 查询订单的充值状态
  77. * @param [string] $orderid [自定义单号]
  78. * @return [array]
  79. */
  80. public function sta($orderid){
  81. $params = 'key='.$this->appkey.'&orderid='.$orderid;
  82. $content = $this->juhecurl($this->staUrl,$params);
  83. return $this->_returnArray($content);
  84. }
  85. /**
  86. * 将JSON内容转为数据,并返回
  87. * @param string $content [内容]
  88. * @return array
  89. */
  90. public function _returnArray($content){
  91. return json_decode($content,true);
  92. }
  93. /**
  94. * 请求接口返回内容
  95. * @param string $url [请求的URL地址]
  96. * @param string $params [请求的参数]
  97. * @param int $ipost [是否采用POST形式]
  98. * @return string
  99. */
  100. public function juhecurl($url,$params=false,$ispost=0){
  101. $httpInfo = array();
  102. $ch = curl_init();
  103. curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
  104. curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
  105. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
  106. curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
  107. curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
  108. if( $ispost )
  109. {
  110. curl_setopt( $ch , CURLOPT_POST , true );
  111. curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
  112. curl_setopt( $ch , CURLOPT_URL , $url );
  113. }
  114. else
  115. {
  116. if($params){
  117. curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
  118. }else{
  119. curl_setopt( $ch , CURLOPT_URL , $url);
  120. }
  121. }
  122. $response = curl_exec( $ch );
  123. if ($response === FALSE) {
  124. //echo "cURL Error: " . curl_error($ch);
  125. return false;
  126. }
  127. $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
  128. $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
  129. curl_close( $ch );
  130. return $response;
  131. }
  132. }