<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>API TEST CACHEDTABLE</title>
<!--meta http-equiv="Content-Type" content="text/html; charset=windows-1250"-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<pre>
<?php

  // adresa Info API servera
  $m_url = 'http://localhost:31000/InfoAPI.aspx';

  // ucitaj pripmer komande u JSON formatu
  $m_json_query = file_get_contents ('json/apf_CACHEDTABLE_query.json');

  // pripremi parametar za poziv servera
	$m_fields['json'] = $m_json_query;

//$m_fields['DebugLevel'] = 255;
//$m_fields['DebugSection'] = "SYSTEM,CLASS,APF_GETARTIKLILIST";

  // pripremi CURL konekciju
  $m_session = curl_init();

  // podesi URL APi servera
	curl_setopt($m_session, CURLOPT_URL, $m_url);

  // podesi broj polja u POST upitu
	curl_setopt($m_session, CURLOPT_POST, count ($m_fields));

	// konvertuj polja upita u HTTP oblik
  $m_fields_query = http_build_query ($m_fields);

  // podesi upit
	curl_setopt($m_session, CURLOPT_POSTFIELDS, $m_fields_query);

  // podesi upit
	curl_setopt($m_session, CURLOPT_RETURNTRANSFER, true);

  // izvrsi upit i preuzmi rezultat
	$m_json_data = curl_exec($m_session);

  // zatvori konekciju
	curl_close($m_session);
	
      $m_output_file = fopen('response.json', 'w');
      fwrite($m_output_file, $m_json_data);
      fclose($m_output_file);
	

  // dekodiraj omotnicu koja je u JSON formatu
  $m_envelope = json_decode ($m_json_data, true);

  // echo "#<pre>m_envelope=";
  // print_r ($m_envelope);
  // echo "</pre>#";

  $m_data = $m_envelope['data'];

	if (! empty ($m_data)) {

    // dekodiraj odgovor koji je u JSON formatu
    $m_response = json_decode ($m_data, true);
 
    if ($m_response['error_state'] == 'ok') {
    	
		$m_database_attachment = json_decode ($m_response['databaseattachment'], true);
		
	    //echo "#<pre>m_database_attachment=";
    	//print_r ($m_database_attachment);
    	//echo "</pre>#";
		 
?>
<table border="1">
	<tr>
<?php
// prikazi nayive polja u zaglavlju
		foreach ($m_database_attachment['items'][0]['fields']['items'] as $m_item) {
?>

<th><?php echo $m_item['name']; ?></th>

<?php
			
		}
?>
</tr>	
<?php
// prikazi slogove
		foreach ($m_database_attachment['items'][0]['rows']['items'] as $m_item) {
?>
<tr>
<td><?php echo $m_item['grupa']; ?></td>
<td><?php echo $m_item['id']; ?></td>
<td><?php echo $m_item['mesto']; ?></td>
<td><?php echo $m_item['opis']; ?></td>
<td><?php echo $m_item['oznaka']; ?></td>
</tr>
<?php
			
		}
?>
</table>
<?

      //$mdatabasedata = base64_decode ($m_response['databaseattachment']);

      //$m_output_file = fopen('data.db', 'w');
      //fwrite($m_output_file, $mdatabasedata);
      //fclose($m_output_file);

      //echo 'data stored to data.db';

      //echo $mdatabasedata;
    } else {
      echo 'error reading data!';
    }


  }
 
?>
</pre>
</body>
</html>