本文實例講述了PHP積分兌換接口的實現方法。分享給大家供大家參考。具體實現方法如下:
exchange.php接口內容如下:
復制代碼 代碼如下:<?php
/*session_start();
if(!isset($_SESSION['zaszh_user_id'])){
echo json_encode(array('status'=>'error','msg'=>'連接超時,請重新打開頁面。'));
exit;
}
$user_id = $_SESSION['zaszh_user_id'];*/
$user_id = 1; // 測試用
$exchange_points = intval($_GET['exchange_points']);
require('connect_database.php');
// 扣除答題積分
$mysqli->query("update zaszh_user set answer_points=answer_points-{$exchange_points} where id='{$user_id}' and answer_points>={$exchange_points}");
if($mysqli->affected_rows){
// 有積分
switch($exchange_points){
// 5元話費
case 200:
$mysqli->query("update zaszh_telephone_charge_surplus set charge_surplus=charge_surplus-5 where date=substring(now(),1,10) and charge_surplus>=5");
if($mysqli->affected_rows){
// 有剩余
$mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','5元話費',unix_timestamp(now()))");
if($mysqli->affected_rows){
echo json_encode(array('status'=>'success','msg'=>'5元話費'));
}else{
// 獲獎失敗
}
}else{
// 無剩余
// 恢復答題積分
$mysqli->query("update zaszh_user set answer_points=answer_points+{$exchange_points} where id='{$user_id}'");
}
break;
// 10元話費
case 400:
$mysqli->query("update zaszh_telephone_charge_surplus set charge_surplus=charge_surplus-10 where date=substring(now(),1,10) and charge_surplus>=10");
if($mysqli->affected_rows){
// 有剩余
$mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','10元話費',unix_timestamp(now()))");
if($mysqli->affected_rows){
echo json_encode(array('status'=>'success','msg'=>'10元話費'));
}else{
// 獲獎失敗
}
}else{
// 無剩余
// 恢復答題積分
$mysqli->query("update zaszh_user set answer_points=answer_points+{$exchange_points} where id='{$user_id}'");
}
break;
}
// 記錄積分消耗
$mysqli->query("insert into zaszh_answer_points_consume(user_id,points_consume,consume_for,create_date) values('{$user_id}','{$exchange_points}','exchange',unix_timestamp(now()))");
}else{
// 無積分
echo json_encode(array('status'=>'error','msg'=>'您的積分不足。'));
}
$mysqli->close();
希望本文所述對大家的php程序設計有所幫助。