<?php

header('content-type: application/json; charset=utf-8');
//en caso de json en vez de jsonp habría que habilitar CORS:
header("access-control-allow-origin: *");


function obtenerIPCliente() {
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // Si existe el encabezado HTTP_X_FORWARDED_FOR, tomamos la primera IP de la lista
        $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
    } else {
        // Si no existe, usamos REMOTE_ADDR
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ip_del_usuario= obtenerIPCliente();


$curl = curl_init();
curl_setopt_array($curl, [
  CURLOPT_URL => "https://geo.leadboxer.com/GeoIpEngine/".$ip_del_usuario."?jsonp=&callback=jQuery112405008093036142254_1706543211533&_=1706543211534",
  CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => [
    "accept: */*",
    "accept-language: es-ES,es;q=0.9",
    "connection: keep-alive",
    "host: geo.leadboxer.com",
    "referer: https://www.opentracker.net/feature/ip-tracker?ip=140.51.159.132",
    "sec-ch-ua: \"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"",
    "sec-ch-ua-mobile: ?0",
    "sec-ch-ua-platform: \"Windows\"",
    "sec-fetch-dest: script",
    "sec-fetch-mode: no-cors",
    "sec-fetch-site: cross-site",
    "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

        $validacion1_p = strpos($response, 'countryCode":"');
                      if ($validacion1_p !== false) {

                      $porciones_1_p = explode('countryCode":"', $response);
                      $porciones_2_p= $porciones_1_p[1];

                      $porciones_3_p = explode('"', $porciones_2_p);
                      $pais = trim($porciones_3_p[0]);
             
         }else{
                      $pais='CO';
         }


         if (strpos($pais, 'VE') !== false) {
         
                    $arr[] = array('s' => '1','z:'=>$pais);
                                  
                    echo '' . json_encode($arr) . '';
                    return;
         }else{

             $arr[] = array('s' => '0','z:'=>$pais);
                                  
                    echo '' . json_encode($arr) . '';
                    return;
               
         } 

?>