<?php
include_once("AllowOrigin.php");
date_default_timezone_set("Asia/Bangkok");
header('Content-Type: application/json');
include_once("libs.php");

// function get_data($url){
//     $curl = curl_init();
//     curl_setopt_array($curl, array(
//     CURLOPT_URL => $url,
//     CURLOPT_RETURNTRANSFER => true,
//     CURLOPT_ENCODING => "",
//     CURLOPT_MAXREDIRS => 10,
//     CURLOPT_TIMEOUT => 30,
//     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//     CURLOPT_CUSTOMREQUEST => "GET",
//     CURLOPT_HTTPHEADER => array(
//         "cache-control: no-cache",
//         "postman-token: 4783b53f-adb2-4fb8-11fc-687d07cb5518"
//     ),
//     ));
//     $response = curl_exec($curl);
//     $err = curl_error($curl);
//     curl_close($curl);
//     return $response;
// }


// function Get_Value()
// {
//     $url = 'http://asia-northeast1-mybot-326003.cloudfunctions.net/nikkei';
//     $variable = get_data($url);
//     $obj = json_decode($variable);
    
//     $result = [];
//     $stock_name = 'นิเคอิ เช้า';
//     $stock_date = '09:30';
// 	$flag = '🇯🇵';
//     $_unit3 = "";
//     $_unit2 = "";
//     foreach ($obj->price as $value) {
//         if ($value[0] === "11:30:00") {

//             $_filter_3 = number_format((float)$value[1], 2, '.', '');
//             $_filter_2 = number_format((float)$obj->prev[0][1], 2, '.', '');

//             $_filter_cal = $_filter_3-$_filter_2;
//             $_filter_cal = explode(".",$_filter_cal);
//             $_unit2 = $_filter_cal[1];

//             $_filter_3 = explode(".",$_filter_3);
//             $_unit3 = substr($_filter_3[0],strlen($_filter_3[0])-1).$_filter_3[1];

//             $result = [
//                 "lottery_loop_name" => $flag.$stock_name.$flag,
//                 "lottery_loop_time" => $stock_date,
//                 "lottery_loop_3unit" => $_unit3,
//                 "lottery_loop_2unit" => $_unit2,
//             ];

//         }
//     }
//     if (empty($result)) {
//         $result = [
//             "lottery_loop_name" => $flag.$stock_name.$flag,
//             "lottery_loop_time" => $stock_date,
//             "lottery_loop_3unit" => 'xxx',
//             "lottery_loop_2unit" => 'xx',
//         ];
//     }
// 	$json = [];
// 	array_push($json, $result);
//     return ['stock' => $json];
// }

// $res = Get_Value();
// echo json_encode($res);



// date_default_timezone_set("Asia/Bangkok");
// function get_dataa($url)
// {
//     $ch = curl_init();
//     curl_setopt($ch, CURLOPT_URL, $url);
//     curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36");
//     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//     curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
//     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//     curl_setopt($ch, CURLOPT_POST, true);
//     curl_setopt($ch, CURLOPT_POSTFIELDS, rawurldecode(http_build_query(array('DateNow' => date("Y-m-d H:i:s")))));
//     $data = curl_exec($ch);
//     curl_close($ch);
//     return $data;
// }

// function remove_empty($array) {
//     return array_filter($array, '_remove_empty_internal');
// }

// function _remove_empty_internal($value) {
//     return !empty($value) || $value === 0;
// }

// function FilterData(){
// 	// $variableee         = get_dataa('https://xn--l3c1aonc.net/%E0%B8%AB%E0%B8%A7%E0%B8%A2%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99/'); // https://หวยสด.net/หวยหุ้น/
// 	// $start              = strpos($variableee,"<span>นิเคอิ (เช้า)</span>");
// 	// $start_cut          = substr($variableee,$start);
// 	// $stop               = strpos($start_cut,"<div  class='lotto-row  row m-0 p-0'>");
// 	// $sub                = strip_tags(substr($start_cut,0,$stop));

//     $variableee         = get_dataa('https://www.huaysod.com/'); // https://หวยสด.net/หวยหุ้น/
// 	$start              = strpos($variableee,"หุ้นนิเคอิรอบเช้า");
// 	$start_cut          = substr($variableee,$start);
// 	$stop               = strpos($start_cut,"co03m co03mx");
// 	$sub                = strip_tags(substr($start_cut,0,$stop));

//     $stock_name = 'นิเคอิ (เช้า)';
//     $stock_date = '09:30';
// 	$flag = '🇯🇵';
// 	$jsonloop = [];

// 	$FilterHTML 		= strip_tags(trim($sub));
// 	$data       		= explode("\n",$FilterHTML);
// 	$trimmedArray 		= array_map('trim', $data);
// 	$filterData 		= [];
// 	foreach ($trimmedArray as $key => $value) {
// 		if(trim($value) != ''){
// 			array_push($filterData,$value);
// 		}
// 	}

// 	if(trim($filterData[2]) == 'รอผล'){$filterData[1] = 'xxx';}
// 	if(trim($filterData[4]) == 'รอผล'){$filterData[3] = 'xx';}
// 	array_push($jsonloop,[
// 		"lottery_loop_name"       => $flag.$stock_name.$flag,
// 		"lottery_loop_time"       => $stock_date,
// 		"lottery_loop_3unit"      => trim($filterData[2]),
// 		"lottery_loop_2unit"      => trim($filterData[4])
// 	]);
//     return ['stock'    => $jsonloop];
// }

// $res = FilterData();
// echo json_encode($res);

// include_once("libs.php");

// ## เปลี่ยนค่าตามผลที่ใช้ ##
// define("_name_","ผลหวย นิคเคอิเช้า");
// // define("_url_","https://lottohub.co/nikkeimorning");
// define("_url_","https://www.lottovip.com/login");
// define("_stock_name_","นิคเคอิเช้า");
// define("_stock_date_","09:30");
// define("_flag_",'🇯🇵');
// ## เปลี่ยนค่าตามผลที่ใช้ ##

// function _load(){
//     $variableee         = get_data(_url_); 
//     $start              = strpos($variableee, '<h5 class="text-truncate">'._name_.'</h5>');
//     $start_cut          = substr($variableee, $start);
//     $stop               = strpos($start_cut, '<div class="card-footer">');
//     $sub                = substr($start_cut, 0, $stop);
//     #filter HTML
//     $FilterHTML         = strip_tags($sub);
//     $data               = explode("\n", $FilterHTML);
//     $trimmedArray       = array_map('trim', $data);
//     $filterData         = [];
//     foreach ($trimmedArray as $key => $value) {
//         if (trim($value) != '') {
//             array_push($filterData, $value);
//         }
//     }
//     // Array ( [0] => ผลหวยหุ้นอียิปต์ [1] => ประจำงวดวันที่ 23 กุมภาพันธ์ 2565 [2] => 3 ตัวบน [3] => 108 [4] => 2 ตัวล่าง [5] => 41 )
// 	$sub_date           = explode(" ", $filterData[1]);
// 	$date 				= date("d");
//     $jsonloop           = [];
// 	if($date == trim($sub_date[1])){
//         array_push($jsonloop, [
//             "lottery_loop_name"     => _flag_._stock_name_._flag_,
//             "lottery_loop_time"     => _stock_date_,
//             "lottery_loop_3unit"    => $filterData[3],
//             "lottery_loop_2unit"    => $filterData[11],
//         ]);
//     }else{
//         array_push($jsonloop, [
//             "lottery_loop_name"     => _flag_._stock_name_._flag_,
//             "lottery_loop_time"     => _stock_date_,
//             "lottery_loop_3unit"    => 'xxx',
//             "lottery_loop_2unit"    => 'xx',
//         ]);
//     }
//     return ['stock' => $jsonloop];
// }

// $res = _load();
// echo json_encode($res);



// function _load(){
//     $variableee         = get_data(_url_); 
//     $start              = strpos($variableee, 'นิเคอิรอบเช้า');
//     $start_cut          = substr($variableee, $start);
//     $stop               = strpos($start_cut, '<div class="carousel-item">');
//     $sub                = substr($start_cut, 0, $stop);
//     #filter HTML
//     $FilterHTML         = strip_tags($sub);
//     $data               = explode("\n", $FilterHTML);
//     $trimmedArray       = array_map('trim', $data);
//     $filterData         = [];
//     foreach ($trimmedArray as $key => $value) {
//         if (trim($value) != '') {
//             array_push($filterData, $value);
//         }
//     }
// // print_r($filterData);
// // Array ( [0] => นิเคอิรอบเช้า [1] => 3ตัวบน [2] => 736 [3] => 2ตัวล่าง [4] => 08 )
//     // Array ( [0] => ผลหวยหุ้นอียิปต์ [1] => ประจำงวดวันที่ 23 กุมภาพันธ์ 2565 [2] => 3 ตัวบน [3] => 108 [4] => 2 ตัวล่าง [5] => 41 )
// 	$sub_date           = explode(" ", $filterData[1]);
// 	$date 				= date("d");
//     $jsonloop           = [];
// 	// if($date == trim($sub_date[1])){
// 	if(trim($filterData[2]) != 'xxx'){
    
//         array_push($jsonloop, [
//             "lottery_loop_name"     => _flag_._stock_name_._flag_,
//             "lottery_loop_time"     => _stock_date_,
//             "lottery_loop_3unit"    => $filterData[2],
//             "lottery_loop_2unit"    => $filterData[4],
//         ]);
//     }else{
//         array_push($jsonloop, [
//             "lottery_loop_name"     => _flag_._stock_name_._flag_,
//             "lottery_loop_time"     => _stock_date_,
//             "lottery_loop_3unit"    => 'xxx',
//             "lottery_loop_2unit"    => 'xx',
//         ]);
//     }
//     return ['stock' => $jsonloop];
// }

// $res = _load();
// echo json_encode($res);




// function get_data($url){
//     $curl = curl_init();
//     curl_setopt_array($curl, array(
//     CURLOPT_URL => $url,
//     CURLOPT_RETURNTRANSFER => true,
//     CURLOPT_ENCODING => "",
//     CURLOPT_MAXREDIRS => 10,
//     CURLOPT_TIMEOUT => 30,
//     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//     CURLOPT_CUSTOMREQUEST => "GET",
//     CURLOPT_HTTPHEADER => array(
//         "cache-control: no-cache",
//         "postman-token: 51464584-baf6-985d-c539-96c559e6ad4d"
//     ),
//     ));
//     $response = curl_exec($curl);
//     $err = curl_error($curl);
//     curl_close($curl);
//     return $response;
// }

// function Get_Value(){
//     $url = 'https://api.kerry888.com/api/home';
//     $variable = get_data($url);
//     $obj = json_decode($variable);
//     $result = [];
//     $stock_name = 'นิคเคอิเช้า';
//     $stock_date = '09:30';
// 	$flag = '🇯🇵';
//     $_unit3 = "xxx";
//     $_unit2 = "xx";

//     foreach ($obj->settrandNoInt->lists as $value) {
//     	if (trim($value->GAME_NAME) === "หุ้นนิเคอิรอบเช้า") {
//         	if($value->RESULT_3UP !== "รอผล" && $value->RESULT_2DOWN !== "รอผล"){
//             	$_unit3 = $value->RESULT_3UP;
//             	$_unit2 = $value->RESULT_2DOWN;
//        		}
//         	// $result = [
//         	// "lottery_loop_name" => $flag.$stock_name.$flag,
//         	// "lottery_loop_time" => $stock_date,
//         	// "lottery_loop_3unit" => $_unit3,
//         	// "lottery_loop_2unit" => $_unit2,
//         	// ]; 
//         }
//     }

// 	// SN 24/12/2023 PP
// 	$_day_of_week = intval(date("N"));
// 	if($_day_of_week === 6 or $_day_of_week === 7) {
//     	$result = [
//            	"lottery_loop_name" => $flag.$stock_name.$flag,
//            	"lottery_loop_time" => $stock_date,
//            	"lottery_loop_3unit" => "xxx",
//            	"lottery_loop_2unit" => "xx",
//         ]; 
//     } else {
//     	$result = [
//            	"lottery_loop_name" => $flag.$stock_name.$flag,
//            	"lottery_loop_time" => $stock_date,
//            	"lottery_loop_3unit" => $_unit3,
//            	"lottery_loop_2unit" => $_unit2,
//         ]; 
//     }
// 	// EN 24/12/2023 PP


// 	$json = [];
// 	array_push($json, $result);
// 	return ['stock' => $json];
// }
// $res = Get_Value();
// echo json_encode($res);



// include "libs.php";

// function _get_()
// {
//     $url = 'https://lottoexp.com/_next/data/_nIKjD1Bo3uuwMkiI81Wu/backward/nikkei-morning.json?lotto=nikkei-morning';
//     $result = [];
//     $stock_name     = 'นิคเคอิเช้า';
//     $stock_date     = '09:30';
//     $flag           = '🇯🇵';
//     $_unit3         = "xxx";
//     $_unit2         = "xx";
//     // $ToDay          = date('Y-d-m');
// 	$ToDay          = date('Y-m-d'); // AP
//     $result         = getResultAPIs($url);
//     $data           = $result->pageProps->data;
//     $StockDay       = explode('T',$data[0]->lottosDate);
//     // if ($StockDay == $ToDay) {
// 	if ($StockDay[0] == $ToDay) { // AP
//         $_unit3 = $data[0]->lottosNumber;
//         $_unit2 = $data[0]->lottosUnder;
//     }
//     $result = [
//         "lottery_loop_name" => $flag . $stock_name . $flag,
//         "lottery_loop_time" => $stock_date,
//         "lottery_loop_3unit" => $_unit3 != null ? $_unit3 : 'xxx',
//         "lottery_loop_2unit" => $_unit2 != null ? $_unit2 : 'xx'
//     ];
//     $json = [];
//     array_push($json, $result);
//     return ['stock' => $json];
// }
// $res = _get_();


// include "libs.php";
// $config = array(
//     'Stock_url'         => 'https://lottoexp.com/_next/data/_nIKjD1Bo3uuwMkiI81Wu/backward/nikkei-morning.json?lotto=nikkei-morning',
//     'Stock_name'        => 'นิคเคอิเช้า',
//     'Stock_date'        => '09:30',
//     'Stock_flag'        => '🇯🇵',
//     'Stock_id'          => 4,
// );
// $res = _get_Web_lottoexp_api($config);
// echo json_encode($res);



// function get_data($url){
//     $curl = curl_init();
//     curl_setopt_array($curl, array(
//     CURLOPT_URL => 'https://indexes.nikkei.co.jp/nkave/get_real_daily_chart?idx=nk225',
//     CURLOPT_RETURNTRANSFER => true,
//     CURLOPT_ENCODING => '',
//     CURLOPT_MAXREDIRS => 10,
//     CURLOPT_TIMEOUT => 0,
//     CURLOPT_FOLLOWLOCATION => true,
//     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//     CURLOPT_CUSTOMREQUEST => 'GET',
//     CURLOPT_HTTPHEADER => array(
//         'Cookie: __utmz=168498041.1714037561.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _gid=GA1.3.1799504286.1714037561; __utma=168498041.651019852.1714037561.1714058216.1714139583.5; __utmc=168498041; __utmt=1; _ga_VNKGMELPRS=GS1.1.1714139583.6.1.1714140626.0.0.0; _ga=GA1.1.651019852.1714037561; __utmb=168498041.12.10.1714139583',
//         'Origin: https://indexes.nikkei.co.jp/',
//         'Referer: https://indexes.nikkei.co.jp/'
//     ),
//     ));

//     $response = curl_exec($curl);
//     curl_close($curl);
//     return $response;
// }


// function Get_Value(){
//     $url = 'https://center.huayded888.com/api/v1/results';
//     $variable = get_data($url);
//     $obj = json_decode($variable);
//     $result = [];
//     $stock_name = 'นิเคอิเช้า';
//     $stock_date = '09:30';
// 	$flag = '🇯🇵';
//     $_unit3 = "xxx";
//     $_unit2 = "xx";
//     echo '<pre>';
//     print_r($variable);

//     // foreach ($obj as $value) {
//     //     if ($value->category === "หวยหุ้น") {
//     //        foreach ($value->data as $value_data) {
//     //         	if($value_data->name === 'หวยหุ้นนิเคอิ (เช้า)'){
//     //             	if($value_data->huayResultModel->result != null ){
//     //           			$_unit3 = $value_data->huayResultModel->result->primaryNumber;
//     //                 	$_unit2 = $value_data->huayResultModel->result->twoNumber;
//     //                 }
//     //                 $result = [
//     //             		"lottery_loop_name" => $flag.$stock_name.$flag,
//     //             		"lottery_loop_time" => $stock_date,
//     //             		"lottery_loop_3unit" => $_unit3,
//     //             		"lottery_loop_2unit" => $_unit2,
//     //         		];   
//     //         	}   
//     //        }
//     //     }
    
//     // }
// 	// $json = [];
// 	// array_push($json, $result);
// 	// return ['stock' => $json];
// }

// $res = Get_Value();
// // echo json_encode($res);



// $file_path = 'nikkei_am.json';
// $json_data = file_get_contents($file_path);
// echo $json_data;

## เปลี่ยนค่าตามผลที่ใช้ ##
define("_name_","ผลหวย นิคเคอิเช้า");
define("_url_","https://stock-nikkei.atmybot.workers.dev/");
define("_stock_name_","นิคเคอิเช้า");
define("_stock_date_","09:30");
define("_flag_",'🇯🇵');
## เปลี่ยนค่าตามผลที่ใช้ ##

//---------------- SN 06.11.2024 PP ------------------
function Get_Value()
{
    // $url = 'https://indexes.nikkei.co.jp/en/nkave/get_real_data?idx=nk225';
    $variable = get_data(_url_);
    // echo '<pre>';
    // print_r($variable);
    $obj = json_decode($variable);
    // print_r($obj);
    $result = [];
    $stock_name = 'นิคเคอิเช้า';
    $stock_date = '09:30';
    $flag = '🇯🇵';
    $unit3 = 'xxx';
    $unit2 = 'xx';




    // เช็คว่ามีคำว่า "(*Morning-Close)" อยู่ใน [datedtime] หรือไม่
    if (strpos($obj->datedtime, "(*Morning-Close)") !== false) {
        //echo "[datedtime] contains '(*Close)'.";

        // ลบส่วนที่ไม่ต้องการออก
        $cleanDate = preg_replace('/\(\*Morning-Close\)/', '', $obj->datedtime);
        $cleanDate = trim($cleanDate); // ลบช่องว่างข้างหน้าและข้างหลัง

        // สร้างวัตถุ DateTime จากวันที่ที่จัดรูปแบบแล้ว
        $dateFromData = DateTime::createFromFormat('M/d/Y', $cleanDate);

        // สร้างวัตถุ DateTime ของวันที่ปัจจุบัน
        $currentDate = new DateTime();

        // เปรียบเทียบวันที่
        if ($dateFromData && $dateFromData->format('Y-m-d') === $currentDate->format('Y-m-d')) {
            //echo "วันที่ตรงกัน";

            // change code unit3 27-05-2567 AP
            // $unit3_edit = number_format("39,381.41", 2, '.', ',');
            // $unit2_edit = number_format($obj->diff, 2, '.', ',');
            // $unit3_real = str_replace([",", "."], "", $unit3_edit);
            // $unit2_real = str_replace([",", "."], "", $unit2_edit);
            // change code unit3 27-05-2567 AP

            $unit3 = $obj->price;
            $unit2 = $obj->diff;



            $result = [
                "lottery_loop_name" => $flag . $stock_name . $flag,
                "lottery_loop_time" => $stock_date,

                // //--------------------------------------

                // "lottery_loop_3unit" => substr($unit3_real, -3), // change code unit3 27-05-2567 AP
                // "lottery_loop_2unit" => substr($unit2_real, -2), // change code unit3 27-05-2567 AP

                // //------------- show all stock ---------
                // "price" => $unit3_edit, // change code unit3 27-05-2567 AP
                // "change" => $unit2_edit  // change code unit3 27-05-2567 AP

                "lottery_loop_3unit" =>getThreeDigits($unit3),
                "lottery_loop_2unit" =>getTwoDigits($unit2),
                "price" => $unit3,
                "change" => $unit2
            ];
        } else {
            $result = [
                "lottery_loop_name" => $flag . $stock_name . $flag,
                "lottery_loop_time" => $stock_date,

                //--------------------------------------

                "lottery_loop_3unit" => 'xxx', // change code unit3 27-05-2567 AP
                "lottery_loop_2unit" => 'xx', // change code unit3 27-05-2567 AP

                //------------- show all stock ---------
                "price" => 'xxx', // change code unit3 27-05-2567 AP
                "change" => 'xx'  //  change code unit3 27-05-2567 AP
            ];
        }

    } else {
        $result = [
            "lottery_loop_name" => $flag . $stock_name . $flag,
            "lottery_loop_time" => $stock_date,

            //--------------------------------------

            "lottery_loop_3unit" => 'xxx', // change code unit3 27-05-2567 AP
            "lottery_loop_2unit" => 'xx', // change code unit3 27-05-2567 AP

            //------------- show all stock ---------
            "price" => 'xxx', // change code unit3 27-05-2567 AP
            "change" => 'xx'  //  change code unit3 27-05-2567 AP
        ];
    }

    // // foreach ($obj as $value) {
    // //     if ($value->category === "หวยหุ้น") {
    // //        foreach ($value->data as $value_data) {
    // //         	if($value_data->name === 'หวยหุ้นนิเคอิ (บ่าย)'){
    // //             	if($value_data->huayResultModel->result != null ){
    // //           			$_unit3 = $value_data->huayResultModel->result->primaryNumber;
    // //                 	$_unit2 = $value_data->huayResultModel->result->twoNumber;
    // //                 }
    // //                 $result = [
    // //             		"lottery_loop_name" => $flag.$stock_name.$flag,
    // //             		"lottery_loop_time" => $stock_date,
    // //             		"lottery_loop_3unit" => $_unit3,
    // //             		"lottery_loop_2unit" => $_unit2,
    // //         		];   
    // //         	}   
    // //        }
    // //     }

    // // }
    // // $json = [];
    // // array_push($json, $result);
    // // return ['stock' => $json];
    $json = [];
    array_push($json, $result);
    return ['stock' => $json];
}

$res = Get_Value();
echo json_encode($res);


//---------------- EN 06.11.2024 PP ------------------


// $file_path = 'nikkei_pm.json';
// $json_data = file_get_contents($file_path);
// echo $json_data;
//-------------------------------------------------------------------
// function get_Api($url)
// {
// 	curl_setopt_array($curl, array(
//   		CURLOPT_URL => 'https://www.raakaadee.com/%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%AB%E0%B8%A7%E0%B8%A2-%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99/%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99%E0%B8%99%E0%B8%B4%E0%B9%80%E0%B8%84%E0%B8%AD%E0%B8%B4%E0%B9%80%E0%B8%8A%E0%B9%89%E0%B8%B2/',
//   		CURLOPT_RETURNTRANSFER => true,
//   		CURLOPT_ENCODING => '',
//   		CURLOPT_MAXREDIRS => 10,
//   		CURLOPT_TIMEOUT => 0,
//   		CURLOPT_FOLLOWLOCATION => true,
//   		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//   		CURLOPT_CUSTOMREQUEST => 'GET',
//   		CURLOPT_HTTPHEADER => array(
//    	 		'Cookie: browser_id=zxnNxs; screen_dia=1920x1080; _ga=GA1.1.1172443157.1753191681; HstCfa4702667=1753191681182; fpestid=_658qzu71K1wezYagMrOplnLLhS2q493iKOZOhr4ZvQchwXhvOs2CHjBA-2_Dc68saZ6WQ; _cc_id=cc855ed0976eec806c65cfa5fd044ae4; __dtsu=6D001741752888C72A5D014A4189D802; c_ref_4702667=https%3A%2F%2Fwww.google.com%2F; HstCmu4702667=1760762103271; panoramaId=f12de86028905010644a84eb9c3016d53938db1fe1f2866dde006fcea02842ee; panoramaIdType=panoIndiv; HstCnv4702667=10; panoramaId_expiry=1763128666270; PHPSESSID=bjvptpma30ubhn1fcggiqd38a2; _ga_YNR4LH6STL=GS2.1.s1762529444$o16$g1$t1762529444$j60$l0$h0; FCCDCF=%5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5B32%2C%22%5B%5C%22e773ee72-3e90-4db1-becd-e23e81926b9f%5C%22%2C%5B1762318530%2C589000000%5D%5D%22%5D%5D%5D; HstCla4702667=1762529445028; HstPn4702667=5; HstPt4702667=31; HstCns4702667=16; FCNEC=%5B%5B%22AKsRol-23o3zwiTFatXgPIBYWnD3lJV_VJLxX83djX4s8Tfj8UcwYbN79XRzqe8cbZiNdX9dxLiGQiHu-esXICcxFUJUBu4sERFpcd5ppvPRMxLDv9_LqzjPXJEV0Fcdg-vOAFpubgT4m0KzZbxrTKp-5BBYrEuKCA%3D%3D%22%5D%5D; PHPSESSID=mljbcbgod0gbiokuo7b6dppe5j',
//     		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
//     		'Accept-Encoding: gzip, deflate, br, zstd',
//     		'Accept-Language: th-TH,th;q=0.9,en;q=0.8',
//     		'Referer: https://www.raakaadee.com/%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%AB%E0%B8%A7%E0%B8%A2-%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99/%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99%E0%B8%99%E0%B8%B4%E0%B9%80%E0%B8%84%E0%B8%AD%E0%B8%B4%E0%B9%80%E0%B8%8A%E0%B9%89%E0%B8%B2/',
//     		'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
//   		),
// 	));

// $response = curl_exec($curl);

// curl_close($curl);
//     return $response;
// }


// function Get_Value()
// {
//     $url = 'https://www.raakaadee.com/%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%AB%E0%B8%A7%E0%B8%A2-%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99/%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99%E0%B8%99%E0%B8%B4%E0%B9%80%E0%B8%84%E0%B8%AD%E0%B8%B4%E0%B9%80%E0%B8%8A%E0%B9%89%E0%B8%B2/';
//     $variable = get_Api($url);
//     echo '<pre>';
//     print_r($variable);
//     $obj = json_decode($variable);
//     // print_r($obj);
//     $result = [];
//     $stock_name = 'นิคเคอิเช้า';
//     $stock_date = '09:30';
//     $flag = '🇯🇵';
//     $unit3 = 'xxx';
//     $unit2 = 'xx';




//     // เช็คว่ามีคำว่า "(*Morning-Close)" อยู่ใน [datedtime] หรือไม่
//     if (strpos($obj->datedtime, "(*Morning-Close)") !== false) {
//         //echo "[datedtime] contains '(*Close)'.";

//         // ลบส่วนที่ไม่ต้องการออก
//         $cleanDate = preg_replace('/\(\*Morning-Close\)/', '', $obj->datedtime);
//         $cleanDate = trim($cleanDate); // ลบช่องว่างข้างหน้าและข้างหลัง

//         // สร้างวัตถุ DateTime จากวันที่ที่จัดรูปแบบแล้ว
//         $dateFromData = DateTime::createFromFormat('M/d/Y', $cleanDate);

//         // สร้างวัตถุ DateTime ของวันที่ปัจจุบัน
//         $currentDate = new DateTime();

//         // เปรียบเทียบวันที่
//         if ($dateFromData && $dateFromData->format('Y-m-d') === $currentDate->format('Y-m-d')) {
//             //echo "วันที่ตรงกัน";

//             // change code unit3 27-05-2567 AP
//             // $unit3_edit = number_format("39,381.41", 2, '.', ',');
//             // $unit2_edit = number_format($obj->diff, 2, '.', ',');
//             // $unit3_real = str_replace([",", "."], "", $unit3_edit);
//             // $unit2_real = str_replace([",", "."], "", $unit2_edit);
//             // change code unit3 27-05-2567 AP

//             $unit3 = $obj->price;
//             $unit2 = $obj->diff;



//             $result = [
//                 "lottery_loop_name" => $flag . $stock_name . $flag,
//                 "lottery_loop_time" => $stock_date,

//                 // //--------------------------------------

//                 // "lottery_loop_3unit" => substr($unit3_real, -3), // change code unit3 27-05-2567 AP
//                 // "lottery_loop_2unit" => substr($unit2_real, -2), // change code unit3 27-05-2567 AP

//                 // //------------- show all stock ---------
//                 // "price" => $unit3_edit, // change code unit3 27-05-2567 AP
//                 // "change" => $unit2_edit  // change code unit3 27-05-2567 AP

//                 "lottery_loop_3unit" =>getThreeDigits($unit3),
//                 "lottery_loop_2unit" =>getTwoDigits($unit2),
//                 "price" => $unit3,
//                 "change" => $unit2
//             ];
//         } else {
//             $result = [
//                 "lottery_loop_name" => $flag . $stock_name . $flag,
//                 "lottery_loop_time" => $stock_date,

//                 //--------------------------------------

//                 "lottery_loop_3unit" => 'xxx', // change code unit3 27-05-2567 AP
//                 "lottery_loop_2unit" => 'xx', // change code unit3 27-05-2567 AP

//                 //------------- show all stock ---------
//                 "price" => 'xxx', // change code unit3 27-05-2567 AP
//                 "change" => 'xx'  //  change code unit3 27-05-2567 AP
//             ];
//         }

//     } else {
//         $result = [
//             "lottery_loop_name" => $flag . $stock_name . $flag,
//             "lottery_loop_time" => $stock_date,

//             //--------------------------------------

//             "lottery_loop_3unit" => 'xxx', // change code unit3 27-05-2567 AP
//             "lottery_loop_2unit" => 'xx', // change code unit3 27-05-2567 AP

//             //------------- show all stock ---------
//             "price" => 'xxx', // change code unit3 27-05-2567 AP
//             "change" => 'xx'  //  change code unit3 27-05-2567 AP
//         ];
//     }

//     // // foreach ($obj as $value) {
//     // //     if ($value->category === "หวยหุ้น") {
//     // //        foreach ($value->data as $value_data) {
//     // //         	if($value_data->name === 'หวยหุ้นนิเคอิ (บ่าย)'){
//     // //             	if($value_data->huayResultModel->result != null ){
//     // //           			$_unit3 = $value_data->huayResultModel->result->primaryNumber;
//     // //                 	$_unit2 = $value_data->huayResultModel->result->twoNumber;
//     // //                 }
//     // //                 $result = [
//     // //             		"lottery_loop_name" => $flag.$stock_name.$flag,
//     // //             		"lottery_loop_time" => $stock_date,
//     // //             		"lottery_loop_3unit" => $_unit3,
//     // //             		"lottery_loop_2unit" => $_unit2,
//     // //         		];   
//     // //         	}   
//     // //        }
//     // //     }

//     // // }
//     // // $json = [];
//     // // array_push($json, $result);
//     // // return ['stock' => $json];
//     $json = [];
//     array_push($json, $result);
//     return ['stock' => $json];
//}

// $res = Get_Value();
// echo json_encode($res);
//----------------------------------------- SN 07/11/2025 PP -------------------------------------------

// ## เปลี่ยนค่าตามผลที่ใช้ ##
// define("_name_","ผลหวย นิคเคอิเช้า");
// define("_url_","https://www.raakaadee.com/%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%AB%E0%B8%A7%E0%B8%A2-%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99/%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99%E0%B8%99%E0%B8%B4%E0%B9%80%E0%B8%84%E0%B8%AD%E0%B8%B4%E0%B9%80%E0%B8%8A%E0%B9%89%E0%B8%B2/");
// define("_stock_name_","นิคเคอิเช้า");
// define("_stock_date_","09:30");
// define("_flag_",'🇯🇵');
// ## เปลี่ยนค่าตามผลที่ใช้ ##

// function get_webpageApi($url)
// {
//     $curl = curl_init();

//     curl_setopt_array($curl, array(
//         CURLOPT_URL => $url,
//         CURLOPT_RETURNTRANSFER => true,
//         CURLOPT_ENCODING => '',
//         CURLOPT_MAXREDIRS => 10,
//         CURLOPT_TIMEOUT => 0,
//         CURLOPT_FOLLOWLOCATION => true,
//         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//         CURLOPT_CUSTOMREQUEST => 'GET',
//         CURLOPT_HTTPHEADER => array(
//             'Cookie: browser_id=zxnNxs; screen_dia=1920x1080; _ga=GA1.1.1172443157.1753191681; HstCfa4702667=1753191681182; fpestid=_658qzu71K1wezYagMrOplnLLhS2q493iKOZOhr4ZvQchwXhvOs2CHjBA-2_Dc68saZ6WQ; _cc_id=cc855ed0976eec806c65cfa5fd044ae4; __dtsu=6D001741752888C72A5D014A4189D802; c_ref_4702667=https%3A%2F%2Fwww.google.com%2F; HstCmu4702667=1760762103271; panoramaId=f12de86028905010644a84eb9c3016d53938db1fe1f2866dde006fcea02842ee; panoramaIdType=panoIndiv; HstCnv4702667=10; panoramaId_expiry=1763128666270; PHPSESSID=bjvptpma30ubhn1fcggiqd38a2; _ga_YNR4LH6STL=GS2.1.s1762529444$o16$g1$t1762529444$j60$l0$h0; FCCDCF=%5Bnull%2Cnull%2Cnull%2Cnull%2Cnull%2Cnull%2C%5B%5B32%2C%22%5B%5C%22e773ee72-3e90-4db1-becd-e23e81926b9f%5C%22%2C%5B1762318530%2C589000000%5D%5D%22%5D%5D%5D; HstCla4702667=1762529445028; HstPn4702667=5; HstPt4702667=31; HstCns4702667=16; FCNEC=%5B%5B%22AKsRol-23o3zwiTFatXgPIBYWnD3lJV_VJLxX83djX4s8Tfj8UcwYbN79XRzqe8cbZiNdX9dxLiGQiHu-esXICcxFUJUBu4sERFpcd5ppvPRMxLDv9_LqzjPXJEV0Fcdg-vOAFpubgT4m0KzZbxrTKp-5BBYrEuKCA%3D%3D%22%5D%5D; PHPSESSID=mljbcbgod0gbiokuo7b6dppe5j',
//             'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
//             'Accept-Encoding: gzip, deflate, br, zstd',
//             'Accept-Language: th-TH,th;q=0.9,en;q=0.8',
//             'Referer: https://www.raakaadee.com/%E0%B8%95%E0%B8%A3%E0%B8%A7%E0%B8%88%E0%B8%AB%E0%B8%A7%E0%B8%A2-%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99/%E0%B8%AB%E0%B8%B8%E0%B9%89%E0%B8%99%E0%B8%99%E0%B8%B4%E0%B9%80%E0%B8%84%E0%B8%AD%E0%B8%B4%E0%B9%80%E0%B8%8A%E0%B9%89%E0%B8%B2/',
//             'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
//         ),
//     ));

//     $response = curl_exec($curl);
//     curl_close($curl);
//     return $response;

// }
// function Get_Value()
// {
//     $res            = get_webpageApi(_url_);
//     $res            = $res . "_____mybot____";
//     $start          = strpos($res, '<a href="/ตรวจหวย-หุ้น/หุ้นนิเคอิเช้า/"');
//     $start_cut      = substr($res, $start);
//     $stop           = strpos($start_cut, '<div id="c-more-cont" style="font-size:24px;">');
//     $sub            = strip_tags(substr($start_cut, 0, $stop));
//     $FilterHTML     = strip_tags(trim($sub));
//     $data           = explode("\n", $FilterHTML);
//     $trimmedArray   = array_map('trim', $data);
//     $server_dt_d    = date('d');
//     $server_dt_m    = date('m');
//     $server_dt_y    = date('Y');
//     $filterData     = [];
//     $jsonloop       = [];
//     $unit_3         = 'xxx';
//     $unit_2         = 'xx';
//     foreach ($trimmedArray as $key => $value) {
//         if (trim($value) != '') {
//             array_push($filterData, $value);
//         }
//     }
//     $time_dt        = explode(' ', $filterData[1]);
//     $server_dt      = intval($server_dt_d) . "/" . $server_dt_m . "/" . $server_dt_y;
//     // $server_dt      = "7/11/2025"; //เอาไว้เทส หลังเที่ยงคืน
//     $api_dt         = intval($time_dt[4]) . "/" . _short_m_to_f2($time_dt[5]) . "/" . THyearShort_to_ENyearFull($time_dt[6]);
//     if ($server_dt == $api_dt) {
//         # code...
//         $unit_3     = trim($filterData[3]);
//         $unit_2     = trim($filterData[7]);
//     }
//     array_push($jsonloop, [
//         "lottery_loop_name"         => _flag_ . _stock_name_ . _flag_,
//         "lottery_loop_time"         => _stock_date_,
//         "lottery_loop_3unit"        => $unit_3,
//         "lottery_loop_2unit"        => $unit_2,
//     ]);
//     return ['stock' => $jsonloop];
// }
// $res = Get_Value();
// echo json_encode($res);

//----------------------------------------- EN 07/11/2025 PP -------------------------------------



// function get_data($url){
//     $curl = curl_init();
//     curl_setopt_array($curl, array(
//     CURLOPT_URL => $url,
//     CURLOPT_RETURNTRANSFER => true,
//     CURLOPT_ENCODING => "",
//     CURLOPT_MAXREDIRS => 10,
//     CURLOPT_TIMEOUT => 30,
//     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//     CURLOPT_CUSTOMREQUEST => "GET",
//     CURLOPT_HTTPHEADER => array(
//         "cache-control: no-cache",
//         "postman-token: 51464584-baf6-985d-c539-96c559e6ad4d"
//     ),
//     ));
//     $response = curl_exec($curl);
//     $err = curl_error($curl);
//     curl_close($curl);
//     return $response;
// }




// function _get_()
// {
//     $url = 'https://stock-nikkei.atmybot.workers.dev/';
//     // strpos($obj->datedtime, "(*Morning-Close)") !== false
//     $result = [];
//     $stock_name     = 'นิคเคอิเช้า';
//     $stock_date     = '09:30';
//     $flag           = '🇯🇵';
//     $_unit3         = "xxx";
//     $_unit2         = "xx";
// 	$ToDay          = date('Y-m-d'); // AP
//     $result         = getResultAPIs($url);
// 	echo "<pre>";

//     // เอา , ออก
// //     $clean = str_replace(',', '', $result->price);

// //     // แปลงเป็นตัวเลขทศนิยม 2 หลัก
// //     $number = number_format((float)$clean, 2, '.', '');

// //     // แยกเป็นส่วนหน้าและทศนิยม
// //     list($intPart, $decimalPart) = explode('.', $number);

// //     // ดึงเลขตัวสุดท้ายของจำนวนเต็ม
// //     $lastInt = substr($intPart, -1);

// //     // รวม 3 หลัก: (1 หลักหน้า + 2 หลักทศนิยม)
// //     $last3 = $lastInt . $decimalPart;

// print_r($result);

// // stdClass Object
// // (
// //     [price] => 50,323.91
// //     [rate] => -0.10%
// //     [diff] => -52.62
// //     [updown_class] => down
// //     [datedtime] => Nov/17/2025(*Close)
// //     [datedtime_md] => Nov/17(*Close)
// //     [open_price] => 50,282.39
// //     [opentime] => (09:00)
// //     [high_price] => 50,398.16
// //     [hightime] => (09:49)
// //     [low_price] => 49,845.86
// //     [lowtime] => (09:16)
// //     [divisor] => 29.91719460
// //     [divisor_date] => Nov/17/2025
// // )

//     if (strpos($result->datedtime, "(*Morning-Close)")  !== false) {



// 	    $result = [
// 	        "lottery_loop_name" => $flag . $stock_name . $flag,
// 	        "lottery_loop_time" => $stock_date,
// 	        "lottery_loop_3unit" => $_unit3 != null ? $_unit3 : 'xxx',
// 	        "lottery_loop_2unit" => $_unit2 != null ? $_unit2 : 'xx'
// 	    ];

//     // 	if (strpos($result->divisor_date, "(*Morning-Close)")  !== false) {
//     // 		// code...
//     // }
//     // print_r($result->datedtime);
//     }

// //     $data           = $result->pageProps->data;
// //     $StockDay       = explode('T',$data[0]->lottosDate);
// //     // if ($StockDay == $ToDay) {
// // 	if ($StockDay[0] == $ToDay) { // AP
// //         $_unit3 = $data[0]->lottosNumber;
// //         $_unit2 = $data[0]->lottosUnder;
// //     }
// //     $result = [
// //         "lottery_loop_name" => $flag . $stock_name . $flag,
// //         "lottery_loop_time" => $stock_date,
// //         "lottery_loop_3unit" => $_unit3 != null ? $_unit3 : 'xxx',
// //         "lottery_loop_2unit" => $_unit2 != null ? $_unit2 : 'xx'
// //     ];
// //     $json = [];
// //     array_push($json, $result);
// //     return ['stock' => $json];
// }
// $res = _get_();
// // echo json_encode($res);






