用户工具

站点工具


demov2

NG接口phpdemo

说明:这是一个基于 PHP 的 NGAPI 接口封装示例,提供了对 NG 游戏 API 的常见操作封装,包括:

代码结构: NewNgApi 类 封装了所有 API 相关的功能。 API URL 配置 在构造函数中定义,包括各个接口的地址。 sendRequest 方法 统一处理 API 请求,使用 cURL 发送 POST 请求,数据格式为 JSON。 示例调用: 直接实例化 NewNgApi,然后调用 gamelogin(“username”) 获取游戏登录地址。 结果以 json_decode($res, true) 格式解析并输出。 如何使用?

<?php
$api = new NewNgApi();
$res = $api->gamelogin("a123456");
print_r(json_decode($res, true));

此代码将获取玩家a123456的游戏登录地址并返回 JSON 格式的响应(注意获取玩家地址前先内部判断玩家是否注册,玩家是否需要转换额度等逻辑操作)。

NewNgApi.php
<?php
date_default_timezone_set('PRC');
 
/**
 * NGAPI PHP Demo class.
 * 
 * @link https://wiki.neapi.com
 * @package NewNgApi
 * @version (PHP 7+, PHP 8+)
 * @author https://wiki.neapi.com
 * @copyright https://www.neapi.com
 * @since 1.0.0
 * 
 */
class NewNgApi
{
    protected $creatememberaccountUrl;                // 创建会员账户
    protected $getbalanceUrl;                         // 查询余额
    protected $getplatformbalanceUrl;                 // 查询全平台余额
    protected $conversionallUrl;                      // 一键转出全平台额度
    protected $gameloginUrl;                          // 获取游戏登录地址
    protected $demologinUrl;                          // 获取demo游戏登录地址
    protected $gameorderUrl;                          // 游戏订单查询
    protected $gamerealtimerecordsUrl;                // 实时游戏记录
    protected $gamehistoryrecordsUrl;                 // 历史游戏记录
    protected $conversionUrl;                         // 额度转换
    protected $conversionStatusUrl;                   // 额度转换状态查询
    protected $gamecodeUrl;                           // 获取游戏代码
    protected $getmerchantBalanceUrl;                 // 查询商户余额
 
    protected $apiUrl;                                // API接口域名
    protected $sn;                                    // 注册开通后商户前缀,请登录管理后台查看
    protected $secretKey;                             // 注册开通后商户密钥,请登录管理后台查看
    protected $platType;                              // 设置平台类型 全部小写
    protected $currency;                              // 币种代码 BRL:巴西雷亚尔,CNY:人民币,HKD:港币,IDR:印度尼西亚盾,INR:印度卢比,MMK:缅甸元,NGN:尼日利亚奈拉,PHP:菲律宾比索,THB:泰铢,USD:美元,VND:越南盾	
    protected $sign;                                  // 32 位小写 md5(random+sn+secretKey)
    protected $random;                                // 随机字符串16-32 位 小写字母 + 数字                          
 
    public function __construct()
    {
        $this->apiUrl = '接口域名';                    // API接口域名
        $this->sn = '商户前缀';                        // 注册开通后商户前缀,请登录管理后台查看  
        $this->secretKey = '商户密钥';                 // 注册开通后商户密钥,请登录管理后台查看
        $this->currency = "CNY";                      // 货币
        $this->platType = 'ag';                       // "https://wiki.neapi.com/doku.php?id=游戏平台" 中的游戏参数
        $this->random = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789'), 0, rand(16, 32));
        $this->sign = md5($this->random . $this->sn . $this->secretKey);
        $this->creatememberaccountUrl = $this->apiUrl . "/api/server/create";
        $this->getbalanceUrl = $this->apiUrl . "/api/server/balance";
        $this->getplatformbalanceUrl = $this->apiUrl . '/api/server/balanceAll';
        $this->conversionallUrl = $this->apiUrl . '/api/server/transferAll';
        $this->gameloginUrl = $this->apiUrl . '/api/server/gameUrl';
        $this->demologinUrl = $this->apiUrl . '/api/server/demoUrl';
        $this->gameorderUrl = $this->apiUrl . '/api/server/recordOrder';
        $this->gamerealtimerecordsUrl = $this->apiUrl . '/api/server/recordAll';
        $this->gamehistoryrecordsUrl = $this->apiUrl . '/api/server/recordHistory';
        $this->conversionUrl = $this->apiUrl . '/api/server/all-credit';
        $this->conversionStatusUrl = $this->apiUrl . '/api/server/record';
        $this->gamecodeUrl = $this->apiUrl . '/api/server/gameCode';
        $this->getmerchantBalanceUrl = $this->apiUrl . '/api/server/quota';
    }
 
    protected $game_type_live = '1';                 //视讯
    protected $game_type_slot = '2';                 //老虎机
    protected $game_type_lottery = '3';              //彩票
    protected $game_type_sports = '4';               //体育
    protected $game_type_esports = '5';              //电竞
    protected $game_type_fishing = '6';              //捕鱼
    protected $game_type_poker = '7';                //棋牌
 
    /**
     * 创建玩家
     * 类型 字段名称 必选 说明
     * @param string $platType 是 游戏平台,参考“游戏平台”附录
     * @param string $playerId 是 玩家账号,长度限制 5-11 位 小写字母 + 数字组合
     * @param string $currency 是 游戏货币,参考“游戏平台”附录
     * mixed
     */
    public function register($username)
    {
        $data = array(
            "playerId" => $username,
            "platType" => $this->platType,
            "currency" => $this->currency
        );
        $res = $this->sendRequest($this->creatememberaccountUrl, $data);
        return $res;
    }
 
    /**
     * 查询余额
     * 类型 字段名称 必选 说明
     *@param platType string 是 游戏平台(参考“游戏平台”附录)
     *@param playerId string 是 玩家账号
     *@param currency string 是 货币代码 BRL:巴西雷亚尔,CNY:人民币,HKD:港币,IDR:印度尼西亚盾,INR:印度卢比,MMK:缅甸元,NGN:尼日利亚奈拉,PHP:菲律宾比索,THB:泰铢,USD:美元,VND:越南盾
     */
    public function getbalance($username)
    {
        $data = array(
            "platType" => $this->platType,
            "playerId" => $username,
            "currency" => $this->currency,
        );
        $res = $this->sendRequest($this->getbalanceUrl, $data);
        return $res;
    }
 
    /**
     * 查询全平台余额
     * 类型 字段名称 必选 说明
     *@param playerId string 是 玩家账号
     *@param currency string 是 货币代码 BRL:巴西雷亚尔,CNY:人民币,HKD:港币,IDR:印度尼西亚盾,INR:印度卢比,MMK:缅甸元,NGN:尼日利亚奈拉,PHP:菲律宾比索,THB:泰铢,USD:美元,VND:越南盾
     */
    public function getbalanceall($username)
    {
        $data = array(
            "playerId" => $username,
            "currency" => $this->currency,
        );
        $res = $this->sendRequest($this->getbalanceUrl, $data);
        return $res;
    }
 
    /**
     *获取游戏登录地址
     *类型 字段名称 必选 说明
     *@param string $platType 游戏平台(参考“游戏平台”附录)(必选)
     *@param string $playerId 玩家账号(必选)
     *@param string $gameType 游戏类型,(1:视讯、2:老虎机、3:彩票、4:体育、5:电竞、6:捕鱼、7:棋牌)(必选)
     *@param string $lang 游戏语言,默认简体中文(参考“游戏语言”附录)
     *@param string $gameCode 游戏代码,默认游戏大厅(参考“游戏平台”附录)
     *@param string $returnUrl 游戏退出时跳转地址,示例:“https://www.neapi.com“
     *@param string $ingress 终端类型,device1:电脑网页版、device2:手机网页版(其他特定终端请参考“游戏平台”附录)(必选)
     *@param string $oddsType 彩票盘口,A:(默认)、B、C,仅IG彩票和SGWin彩票可选
     *@return mixed
     */
    public function gamelogin($username, $lang = "", $gameCode = "", $returnUrl = "", $ingress = 'device1', $oddsType = "")
    {
        $data = array(
            "platType" => $this->platType,
            "playerId" => $username,
            "gameType" => $this->game_type_live,
            "currency" => $this->currency,
            "lang" => $lang,
            "gameCode" => $gameCode,
            "returnUrl" => $returnUrl,
            "ingress" => $ingress,
            "oddsType" => $oddsType
        );
        $res = $this->sendRequest($this->gameloginUrl, $data);
        return $res;
    }
 
    /**
     * 获取实时下注记录
     * 类型 字段名称 必选 说明
     * @param $currency
     * @param $pageNo
     * @param int $pageSize 
     * @return mixed
     * 
     */
    public function gamerealtimerecords($page = 1, $pageSize = 200)
    {
        $data = array(
            "currency" => $this->currency,
            "pageNo" => $page,
            "pageSize" => $pageSize,
        );
        $res = $this->sendRequest($this->gamerealtimerecordsUrl, $data);
        return $res;
    }
 
    /**
     *获取游戏代码
     *@param string $platType  游戏平台,参考“游戏平台”附录
     *@return mixed
     */
    public function gamecode()
    {
        $data = array(
            "platType" => $this->platType
        );
        $res = $this->sendRequest($this->gamecodeUrl, $data);
        return $res;
    }
 
    /**
     *@return mixed
     *查询商户平台额度
     */
    public function quota()
    {
        $data = array();
        $res = $this->sendRequest($this->getmerchantBalanceUrl, $data);
        return $res;
    }
 
 
    private function sendRequest($url, $data = array())
    {
        // 请求头
        $header = [
            'sn:' . $this->sn, // 如果需要身份验证
            'sign:' . $this->sign,
            'random:' . $this->random,
            'Content-Type: application/json',                 //请求类型
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, empty($data) ? "{}" : json_encode($data));
        $contents = curl_exec($ch);
        curl_close($ch);
        return $contents;
    }
}
 
/**
 * 使用demo
 */
// $api = new NewNgApi();
// $res = $api->register("username");
 
$api = new NewNgApi();
$res = $api->gamelogin("username");
echo $res; //默认返回json(参考文档各API返回内容)
 
//print_r(json_decode($res, true)); //数组形式返回
demov2.txt · 最后更改: 2025/02/08 01:21 (外部编辑)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki