本代码是基于聚合数据的话费充值API实现的话费充值功能,使用前需要:
一、引入封装的代码类//----------------------------------
// 聚合数据-手机话费充值API调用示例代码
//----------------------------------
header('Content-type:text/html;charset=utf-8');
include 'class.juhe.recharge.php'; //引入文件
二、配置一些必须的参数//接口基本信息配置
$appkey = '291bf7184**********************'; //从聚合申请的话费充值appkey
$openid = 'JH8d954266539************'; //注册聚合账号就会分配的openid,在个人中心可以查看
$recharge = new recharge($appkey,$openid);
三、检测手机号码以及面额是否可以充值
$telCheckRes = $recharge->telcheck('18913511234',10);
if($telCheckRes){
//说明支持充值,可以继续充值操作,以下可以根据实际需求修改
echo "OK";
}else{
//暂不支持充值,以下可以根据实际需求修改
exit("对不起,该面额暂不支持充值");
}
四、根据手机号码以及面额查询商品信息
$telQueryRes =$recharge->telquery('189********',10); #可以选择的面额5、10、20、30、50、100、300
if($telQueryRes['error_code'] == '0'){
//正常获取到话费商品信息
$proinfo = $telQueryRes['result'];
/*
[cardid] => 191406
[cardname] => 江苏电信话费10元直充
[inprice] => 10.02
[game_area] => 江苏苏州电信
*/
echo "商品ID:".$proinfo['cardid']."
";
echo "商品名称:".$proinfo['cardname']."
";
echo "进价:".$proinfo['inprice']."
";
echo "手机归属地:".$proinfo['game_area']."
";
}else{
//查询失败,可能维护、不支持面额等情况
echo $telQueryRes["error_code"].":".$telQueryRes['reason'];
}
五、提交话费充值$orderid = '111111111'; //自己定义一个订单号,需要保证唯一
$telRechargeRes = $recharge->telcz('189********',5,$orderid); #可以选择的面额5、10、20、30、50、100、300
if($telRechargeRes['error_code'] =='0'){
//订单提交成功,可以根据实际需求改写以下内容
echo "订单提交成功,订单号:".$telRechargeRes['result']['sporder_id'];
var_dump($telRechargeRes);
}else{
//提交充值失败,具体可以参考$telRechargeRes['reason']
var_dump($telRechargeRes);
}
六、订单状态查询
$orderid = '111111111'; //商家自定的订单号
$orderStatusRes = $recharge->sta($orderid);
if($orderStatusRes['error_code'] == '0'){
//查询成功
if($orderStatusRes['result']['game_state'] =='1'){
echo "充值成功";
}elseif($orderStatusRes['result']['game_state'] =='9'){
echo "充值失败";
}elseif($orderStatusRes['result']['game_state'] =='-1'){
echo "提交充值失败"; //可能是如运营商维护、账户余额不足等情况
}
}else{
//查询失败
echo "查询失败:".$orderStatusRes['reason']."(".$orderStatusRes['error_code'].")";
}
七、完整的class.juhe.recharge.php代码
// +----------------------------------------------------------------------
//----------------------------------
// 聚合数据-手机话费充值API调用类
//----------------------------------
class recharge {
private $appkey;
private $openid;
private $telCheckUrl = 'http://op.juhe.cn/ofpay/mobile/telcheck';
private $telQueryUrl = 'http://op.juhe.cn/ofpay/mobile/telquery';
private $submitUrl = 'http://op.juhe.cn/ofpay/mobile/onlineorder';
private $staUrl = 'http://op.juhe.cn/ofpay/mobile/ordersta';
public function __construct($appkey,$openid){
$this->appkey = $appkey;
$this->openid = $openid;
}
/**
* 根据手机号码及面额查询是否支持充值
* @param string $mobile [手机号码]
* @param int $pervalue [充值金额]
* @return boolean
*/
public function telcheck($mobile,$pervalue){
$params = 'key='.$this->appkey.'&phoneno='.$mobile.'&cardnum='.$pervalue;
$content = $this->juhecurl($this->telCheckUrl,$params);
$result = $this->_returnArray($content);
if($result['error_code'] == '0'){
return true;
}else{
return false;
}
}
/**
* 根据手机号码和面额获取商品信息
* @param string $mobile [手机号码]
* @param int $pervalue [充值金额]
* @return array
*/
public function telquery($mobile,$pervalue){
$params = 'key='.$this->appkey.'&phoneno='.$mobile.'&cardnum='.$pervalue;
$content = $this->juhecurl($this->telQueryUrl,$params);
return $this->_returnArray($content);
}
/**
* 提交话费充值
* @param [string] $mobile [手机号码]
* @param [int] $pervalue [充值面额]
* @param [string] $orderid [自定义单号]
* @return [array]
*/
public function telcz($mobile,$pervalue,$orderid){
$sign = md5($this->openid.$this->appkey.$mobile.$pervalue.$orderid);//校验值计算
$params = array(
'key' => $this->appkey,
'phoneno' => $mobile,
'cardnum' => $pervalue,
'orderid' => $orderid,
'sign' => $sign
);
$content = $this->juhecurl($this->submitUrl,$params,1);
return $this->_returnArray($content);
}
/**
* 查询订单的充值状态
* @param [string] $orderid [自定义单号]
* @return [array]
*/
public function sta($orderid){
$params = 'key='.$this->appkey.'&orderid='.$orderid;
$content = $this->juhecurl($this->staUrl,$params);
return $this->_returnArray($content);
}
/**
* 将JSON内容转为数据,并返回
* @param string $content [内容]
* @return array
*/
public function _returnArray($content){
return json_decode($content,true);
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
public function juhecurl($url,$params=false,$ispost=0){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
}