<?php include "../../config.php"; ?>
<?php include "../../assets/var.php"; ?>
<?php include "../../assets/function.php"; ?>

<?php $phone_code = [ '+6221', '+6222', '+6223', '+6224', '+6225', '+6226', '+6227', '+6228', '+6229', '+6230', '+6231', '+6232', '+6233', '+6234', '+6235', '+6236', '+6237', '+6238', '+6239', '+6240', '+6241', '+6242', '+6243', '+6244', '+6245', '+6246', '+6247', '+6248', '+6249', '+6250', '+6251', '+6252', '+6253', '+6254', '+6255', '+6256', '+6257', '+6258', '+6259', '+6260', '+6261', '+6262', '+6263', '+6264', '+6265', '+6266', '+6267', '+6268', '+6269', '+6270', '+6271', '+6272', '+6273', '+6274', '+6275', '+6276', '+6277', '+6278', '+6279', '+6290', '+6291', '+6292', '+6293', '+6294', '+6295', '+6296', '+6297', '+6298', '+6299' ]; ?>

<?php
$sql  = $pdo->query("SELECT * FROM tb_keyword_scrapping_maps");
$data = $sql->fetch(PDO::FETCH_ASSOC);

$keyword = $data['keyword'];
$ll      = $data['ll'];
//$api_key = $data['api_key'];
$api_key = '';//google api active

$sql_    = $pdo->query("SELECT * FROM tb_listing_maps where keyword = '".$keyword."'");

$start = $sql_->rowCount()+1;

$field = json_encode([
  	/*"country" => "ID",
  	"domain"  => "com",
  	"keyword" => $keyword,
  	"ll"      => $ll,
  	"start"   => $start*/
  	]);
?>
<!--############################# Curl ################################-->

<?php
$myfile = fopen("pagetoken.txt", "r") or die("Unable to open file!");
if (filesize('pagetoken.txt') != 0)
{
$pagetoken= fread($myfile,filesize("pagetoken.txt"));

}
fclose($myfile);
?>

<?php
$curl = curl_init();
  
curl_setopt_array($curl, array(
   
	CURLOPT_URL => 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . urlencode($keyword) . '&key=' .$api_key . '&pagetoken=' . @$pagetoken,
	
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $field,
    CURLOPT_HTTPHEADER => array(
        'x-api-key: ' . $api_key . '',
        'Content-Type: application/json'
    ),
));
  
$response = curl_exec($curl);
curl_close($curl);

$data                = json_decode($response, true);
$response_            = json_decode($response);

$scrapingResultCount = count($data['results']);

$latitude = $response_->results[0]->geometry->location->lat;
$longitude = $response_->results[0]->geometry->location->lng;

$ll = $latitude . "," . $longitude;
    
if($data['status'] != 'OK')
{
    session_start();
    unset($_SESSION['extract']);
    $extract = "Selesai";
}
else
{
    if($data['status'] == 'OK')
    {
        $extract = "Sedang berproses ..";
    }
}
    
echo "Status: " . $data['status'] . "<br>";
echo "Pesan: " . $data['message'] . "<br>";
echo "Ektrak: " . $extract . "";
	
for ( $z = 0; $z < $scrapingResultCount; $z++ ) 
{
    
    $no = $z+1;
    $place_id  = $data['results'][$z]['place_id'];
	$name      = $data['results'][$z]['name'];
	
	$place_id_query = "https://maps.googleapis.com/maps/api/place/details/json?placeid={$place_id}&key=" . $api_key;

	$response_str = file_get_contents( $place_id_query );
    
    if ( $response_str ) {
        
		$response = @json_decode( $response_str,true );
    
	$phone_old  = $response['result']['international_phone_number'];
    $phone      = str_replace(str_split('\\/:*?"<>|- '), '', $phone_old);
	    
	if( $response && $phone_old != null && !in_array(substr($phone,0,5),$phone_code))
	{
	    $sql_exist  = $pdo->query("SELECT * FROM tb_listing_maps where phone_old = '".$phone_old."'");
        
        if($sql_exist->rowCount() == 0 )
        {

	    $pdo->query ("insert into tb_listing_maps
        (
        title,
        phone,
        phone_old,
        keyword,
        ll,
        posted
        )
        values 
            (
            '".$response['result']['name']."',
            '".$phone."',
            '".$phone_old."',
            '".$keyword."',
            '".$ll."',
            '".date('Y-m-d H:i:s')."'
            )
             ");
             
        }
             
	}
	
    }
             
}

?>

<?php
$myfile = fopen("pagetoken.txt", "w") or die("Unable to open file!");
$txt = @$data['next_page_token'];
fwrite($myfile, $txt);
fclose($myfile);
?>