
<?php
$id=$_GET["id"];
$precio=$_GET["precio"];

$url = "https://192.168.16.200/jsonPTS";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_USERPWD, "jpanta:gigi240602");
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: application/json",
//   "Authorization: Basic YWRtaW46YWRtaW4=",
   "Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = <<<DATA
{
 "Protocol":"jsonPTS",
 "Packets":[{
"Id":1,
 "Type":"GetFuelGradesConfiguration"
}]
}
DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp=curl_exec($curl);
$respuesta=json_decode($resp,true);
$cabset='{"Protocol":"jsonPTS","Packets":[{ "Id":1, "Type":"SetFuelGradesConfiguration", "Data":{ "FuelGrades":[';
$cuerposet='';
$combs=$respuesta['Packets'][0]['Data']['FuelGrades'];
foreach($combs as $comb){
        if (intval($id)==intval($comb['Id'])){ $cuerposet=$cuerposet.'{"Id":'.$comb['Id'].', "Name":'.$comb['Name'].', "Price":'.$precio.'},';}
        else {$cuerposet=$cuerposet.'{"Id":'.$comb['Id'].', "Name":'.$comb['Name'].', "Price":'.$comb['Price'].'},';}
}
$pieset=']}}]}';
curl_close($curl);
$vGestor=fopen("/var/www/html/jsonPTS/logs/logprecios".date("Ymd").".txt", "a");
fwrite($vGestor, date("d/m/Y H:i:s")."\t\t".$cabset.$cuerposet.$pieset."\r\n");
fclose($vGestor);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_USERPWD, "jpanta:gigi240602");
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = $cabset.$cuerposet.$pieset;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$resp=curl_exec($curl);
$respuesta=json_decode($resp,true);
curl_close($curl);

//for debug only!
echo $resp;
?>
