<?php
//include("modules/db.php");

if (isset($_GET['term'])) {
     
   // $sql = "SELECT * FROM products WHERE pro_name LIKE '%{$_GET['term']}%' LIMIT 25";
   //  $db = new DB();
   //  $conn = $db->DBConnect();
   //  $result = $conn->query($sql);
    
   //  while($row = $result->fetch_assoc()) {
   //    $res[] = $row["pro_name"];
   //  }
   // echo json_encode($res);

    $post = [
		    'dataType' => 'json',
		    'keyword' => $_GET['term']
	];
	$ch = curl_init();
	// Will return the response, if false it print the response
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	// Set the url
	curl_setopt($ch, CURLOPT_URL,"https://www.advice.co.th/advice-search");
	
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
	
	
	// Execute
	$response = curl_exec($ch);

	foreach (json_decode($response, true) as $key => $value) {
		$res[] = $value["name"];
	}
	echo json_encode($res);
}
?>