全国彩票开奖结果查询-PHP版

一、简单说明

使用聚合数据提供的免费全国彩票开奖结果查询接口服务,目前支持双色球、大乐透、七乐彩、七星彩、福彩3D、排列3、排列5,数据来源于网络公开,开奖结果获取可能会有一定的延时。
官网接口文档地址:https://www.juhe.cn/docs/api/id/300,注册申请即可获得接口请求Key。

二、PHP调用代码参考

Lottery.class.php

/**
* Class Lottery
*/
class Lottery
   {
//接口提交地址
private static $submitUrl = 'http://apis.juhe.cn/lottery';

//申请的彩票接口请求Key
private static $appkey = '**********';

/**
 * 获取支持彩票列表
 */
public static function getLotteryTypes()
{
    $urlPath = '/types';
    $params = [
        'key' => self::$appkey
    ];
    $paramsString = http_build_query($params);

    $requestUrl = self::$submitUrl.$urlPath;
    $content = self::juheCurl($requestUrl, $paramsString);
    $result = json_decode($content, true);
    if ($result) {
        return $result;
    } else {
        return false;
    }
}

/**
 * 获取彩票某一期开奖结果,默认最新一期
 * @param $lotteryId 彩票ID
 * @param string $lotteryNo 彩票期数,默认最新一期
 * @return bool|mixed
 */
public static function getLotteryRes($lotteryId, $lotteryNo = "")
{
    $urlPath = '/query';

    $params = [
        'key' => self::$appkey,
        'lottery_id' => $lotteryId,
        'lottery_no' => $lotteryNo
    ];
    $paramsString = http_build_query($params);

    $requestUrl = self::$submitUrl.$urlPath;
    $content = self::juheCurl($requestUrl, $paramsString);
    $result = json_decode($content, true);
    if ($result) {
        return $result;
    } else {
        return false;
    }
}

/**
 * 获取历史开奖结果
 * @param $lotteryId 彩票ID
 * @param int $pageSize 每页返回条数
 * @param int $page 当前页数
 */
public static function getLotteryHistroyRes($lotteryId, $pageSize = 10, $page = 1)
{
    $urlPath = '/history';

    $params = [
        'key' => self::$appkey,
        'lottery_id' => $lotteryId,
        'page_size' => $pageSize,
        'page' => $page,
    ];
    $paramsString = http_build_query($params);

    $requestUrl = self::$submitUrl.$urlPath;
    $content = self::juheCurl($requestUrl, $paramsString);
    $result = json_decode($content, true);
    if ($result) {
        return $result;
    } else {
        return false;
    }
}

/**
 * 中奖计算器/判断号码是否中奖
 * @param $lotteryId 彩票ID
 * @param $lotteryRes 投注号码
 * @param string $lotteryNo 投注期号,默认最新一期
 * @return bool|mixed
 */
public static function getBonus($lotteryId, $lotteryRes, $lotteryNo='')
{
    $urlPath = '/bonus';

    $params = [
        'key' => self::$appkey,
        'lottery_id' => $lotteryId,
        'lottery_res' => $lotteryRes,
        'lottery_no' => $lotteryNo,
    ];
    $paramsString = http_build_query($params);

    $requestUrl = self::$submitUrl.$urlPath;
    $content = self::juheCurl($requestUrl, $paramsString);
    $result = json_decode($content, true);
    if ($result) {
        return $result;
    } else {
        return false;
    }
}

/**
 * 发起接口网络请求
 * @param $url
 * @param bool $params
 * @param int $ispost
 * @return bool|mixed
 */
public static 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, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 12);
    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) {
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}
}

index.php

include "Lottery.class.php";

/**
* 获取支持彩票列表
*/
$lotteryTypes = Lottery::getLotteryTypes();
if ($lotteryTypes) {
print_r($lotteryTypes);
}

/**
* 获取某一期开奖结果
*/
$lotteryResult = Lottery::getLotteryRes('ssq', '18028');
if ($lotteryResult) {
print_r($lotteryResult);
}

/**
* 获取彩票历史开奖结果
*/
$lotteryHistoryResult = Lottery::getLotteryHistroyRes('ssq', 10 ,1);
if ($lotteryHistoryResult) {
print_r($lotteryHistoryResult);
}


/**
* 中奖计算器/判断号码是否中奖
*/
$lotteryBonusResult = Lottery::getBonus('ssq', '13,14,20,21,25,33@07', '18030');
if ($lotteryBonusResult) {
print_r($lotteryBonusResult);
}

你可能感兴趣的:(php)