<?php
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);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

function Get_data() {
  $url          = '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/';
  $variable     = get_dataa($url);
//   echo '<pre>';
//   print_r($variable);
  $start        = strpos($variable,'<span>หุ้นอินเดีย</span>');
  $start_cut    = substr($variable,$start);
  $stop         = strpos($start_cut,'<span>หุ้นเยอรมัน</span>');
  $sub          = substr($start_cut,0,$stop); 
  $remove_th    = str_replace("<div class='lotto-result col-2 pt-2'>","",$sub);
  $remove_th    = str_replace("<div  class='lotto-row  row m-0 p-0'>","",$remove_th);
  $remove_th    = str_replace("<div class='lotto-col d-flex col-6 align-middle'>","",$remove_th);
  $remove_th    = str_replace("</span>","\n#",$remove_th);
  $remove_th    = str_replace("<span>","",$remove_th);
  $remove_th    = str_replace("<div class='live col-2'>"," ",$remove_th);
  $remove_th    = str_replace("<div class='min col-1'>","",$remove_th);
  $remove_th    = str_replace("<div class='col-8 px-5 pt-2'>","",$remove_th);
  $remove_th    = str_replace("</div>","",$remove_th);




  $remove_th    = preg_replace("/\r|\n/", "", $remove_th);
  $data         = explode('#',$remove_th);
//     echo '<pre>';
//   print_r($data);
  return $data;
}

function Build_Json() {
    # code...
    $result                       = Get_data();
    $jsonloop                     = [];
    array_push($jsonloop,[
        "lottery_loop_name"       => trim($result[0]),
        "lottery_loop_time"       => trim('17:30'),
        "lottery_loop_3unit"      => trim($result[1]) == 'รอผล' ? 'xxx' : trim($result[1]),
        "lottery_loop_2unit"      => trim($result[3]) == 'รอผล' ? 'xx' : trim($result[3])
    ]);
    return ['stock'          => $jsonloop];
}

$res = Build_Json();
echo json_encode($res);
// Get_data();
