<pre>
<?php

$m_opt = "1"; // upit nad objektom 0101, pozicija SPEKTAR11
//$m_opt = "2"; // upit nad objektima 5101,5102,5102, pozicija SPEKTAR12

  if ($m_opt == "1") {

    // korisnicko ime
    $m_username = "spektar1";

    // lozinka
    $m_password = "spektar";

    // objekat
    $m_sif_obj_cond = "0101";

     // filterski uslov: cena <> 0
    $m_filter = '\"exp\": \"NE\",\"params\": {\"items\": [\"cena\",\"0\"]}';

  } else {

    // korisnicko ime
    $m_username = "spektar2";

    // lozinka
    $m_password = "spektar";

    // lista objekata
    $m_sif_obj_cond = "5101,5102,5102";

    // filterski uslov: stanje > 0
    $m_filter = '\"exp\": \"GT\",\"params\": {\"items\": [\"stanje\",\"0\"]}';

  }

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

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

  $m_json_query = str_replace (array ('##username##', '##password##', '##sif_obj_cond##', '##filter##'), array ($m_username, $m_password, $m_sif_obj_cond, $m_filter), $m_json_query);

//echo $m_json_query;
  

  // 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);

  // 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);
 
    // echo "#<pre>m_response=";
    // print_r ($m_response);
    // echo "</pre>#";

    if ($m_response['error_state'] == 'ok') {

      $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>