How to develop JSON with PHP

JSON is pretty flexible when you want to pull data from MySQL database and feed it to the front end software such as Web-based applications or Desktop software or even Mobile applications like Android, iPhone, iPad. I’ll just give you a simple example that is running PHP to feed data to a front end app.

On the Android, you’ll build an interface that let user input a word in English (for example: eat), then hit the Submit button to translate this word into Italian and Vietnamese. Once the request transfer to the server, PHP will process the data and return an object that include a word with the same meaning in Italian and English. This is what you’ll build in your PHP page, so that Android app can call that page and pass the parameter as GET to the server.

switch(strtoupper($_SERVER['REQUEST_METHOD'])){
 case 'GET':{
  $english = strtolower(trim($_REQUEST['par']));
  $t = $Db->GetRow("select * from $database_mns.android_evi_dictionary where english = @1@", $english);

  //build array lists of keys and values to feed the Object
  $ar_keys = array('comp_logo', 'developer_name', 'ip_address', 'english', 'italian', 'vietnamese');
  $ar_values = array('www.YourDomain.com/images/logo_jpg.jpg', 'Jane Doe', $_SERVER["REMOTE_ADDR"], $t['english'], $t['italian'], iconv("UTF-8", "ISO-8859-1", $t['Italian']));

  $obj = new stdClass();
  for($i=0;$i<count($ar_keys); $i++){
   $obj->{$ar_keys[$i]} = $ar_values[$i];
  }
  echo json_encode($obj);
  break;
 }
 default:{
  header('HTTP/1.0 405 Method Not Allowed');
 }
}