------------------------------------------

InterOperability Using Json or YML

------------------------------------------

InterOperability means : Communication between two independent language

using API's

Mode of Channel : Internet (WEB-API)

data Format :XML,JSON,YML (Highly Recommended)


APP1 (PHP)  <----API-----> APP2 (JAVA)

Language A <-----data-----> Language B

API: Application Programming Interface.


Why Api?

------


Note : How to Add Password configuration in PHPMYADMIN

gote Xampp/phpmyadmin/config.inc.php

    

change the Authentication Type from config to cookie

/* Authentication type and info */

$cfg['Server'][$i]['auth_type'] = 'cookie'; #config

 

Sample of API Code with API_KEY

API_KEY has following names Auth_key,Secret_key, Auth_token,API_TOKEN,..etc

But Industry Standard is based Upon JWT(Json Web Tokens)


Data Representation using JSON

----------------

JSON : JavaScript Object Notation

It is common mode of data Transmisson or Representation


Few Terms in JSON

----------------

JSON Data is Combination of key and value pair

JSON object

{

"key":"value"

}


JSON Object by Multiple Keys and value

JSON Data

{

   "key":"value",

   "name":"ravi",

   "class":12,

   "marks":null,

   "passed":true,

   }


How to make Multiple Data in JSON Object

------------------------

JSON ARRAY

{

  "Hobbies":["singing","dancing","crying","Not Bathing"]

}

   

   Student for Ravi using JSON

   ------------------

   Student : Ravi

   {

     "key":"value",

     "name":"ravi",

     "class":12,

     "marks":null,

     "passed":true,

     "Hobbies":["singing","dancing","crying","Not Bathing"],

   }

   

     Student : Rahul

   {

     "key":"value",

     "name":"Rahul",

     "class":10,

     "marks":10,

     "passed":false,

     "Hobbies":["singing","dancing","crying","Bathing"],

   }


ARRAY of OBJECTS

{

  "data":[

       {

     "key":"value",

     "name":"ravi",

     "class":12,

     "marks":null,

     "passed":true,

     "Hobbies":["singing","dancing","crying","Not Bathing"],

   },

   

      {

     "key":"value",

     "name":"Rahul",

     "class":10,

     "marks":10,

     "passed":false,

     "Hobbies":["singing","dancing","crying","Bathing"],

   },

]

}


Serialise and deserialise

-------------------------

PHP Object with StdClass

Serialisation : Coverting One type of Object to another type of Object

PHP Object coverting to JSON Object

API ENCODING


if you are using API then we must set Content-Type using headers



How to check Response using javaScript

----------

document.contentType

"application/json"


How to write json Response to JSON File

for that we have two ways

By request to JSON url

http://localhost:8000/php-class/InterOperability/objects.json

.json is extension


1. Either Create Objects.json file and write the data to the File and you can access thet file from url

2. RewriteRule of URL REWRITTING using htaccess


RewiteEngine On

RewriteRule ^([a-zA-Z0-9-_]+).json$ objects.php


How to create Dynamic Json file using php

Explaination : using File Handling


step1: fopen the file

fopen("filename","mode")

mode => r,r+,w,w+,a,a+


+plus : extended

without+ mode : strict Mode


r : read only No write Operation : Strict Mode

r+ : read and write operation : Write extended


step2 : Intialise the file pointer

$fp = fopen("output.json","r");


steps 3:

check if $fp is Null

if($fp==NULL){

  die();

  exit();

}


Debugging Functions:

------------------

These functions are used for debugging point of view

1. die() : It terminate the execution of script at a perticular Point with any message

2. exit() : same as die(): The only difference is exit support 0-255 Index Numbers.

        Exit is language Construct so you can use without parenthesis

exit;

3. vardump(): It can dump everything about a varible or varible dumping

  $arr = array(10,"20",true,[10,20,30],10.5);

  

  var_dump($arr);

  $arr is a Array

  [0=>int(10),1=>string(2)"20",3=>boolean(true),4=>Array[0=>int(10),1=>int(20),2=>int(30),],5=>double(10.5)];

  

  Q.1.   Difference B/W echo and var_dump

  1. echo cannot print Array

  2. var_dump can print Array or Array_father

  

  1. echo cannot print Boolean False

  2. var_dump can print boolean false or boolean false_father

  

  1. echo is language construct

  2. var_dump is not language construct but support multiple argument under single parenthesis

  

  Q.2.   Make a user defined function called prx() print and exit for debugging purpose
  it can print any type of Datatype in a formattable order

  

  function prx($arg){

     echo "<pre>";

if(is_array($arg)){

   print_r($arg);

}elseif(is_null($arg)or is_bool($arg) or is_object($arg)){

   var_dump($arg);

}else{

   echo $arg;

}

echo "</pre>";

exit;

  }


   function pr($arg){

     echo "<pre>";

if(is_array($arg)){

   print_r($arg);

}elseif(is_null($arg)or is_bool($arg) or is_object($arg)){

   var_dump($arg);

}else{

   echo $arg;

}

echo "</pre>";

  }


print_r :


<?php

#print_r for debugging means printing in a Formatted way


$arr = array(10,20,30);

echo "<pre style='background-color:black;color:white;font-family:rockwell;'>";

print_r($arr);

echo "</pre>";


After Checking for Null

-------------------------

You can perform read write operation

for example

read: fread

write: fwrite


read

fread($fp);


fwrite($fp,"Line of code");

close the file

fclose($fp);


Writing the data at once:

file_put_contents("output.json",$json);


Reading the data at Once:

$data=file_get_contents("Output.json")

var_dump($data);


#wap in PHP to read/write the Data AT once in a File for Api

Write:

<?php

$student = new Stdclass();

$student->name = "ravi";

$student->mobile = "7765677578";

$student->email = "ravi@gmail.com";


//Encode it into Json

header("Content-Type:application/json");

$json=json_encode($student,JSON_PRETTY_PRINT);


//Used to write Complete Data into a file

file_put_contents("output.json",$json);


Read:

$data=file_get_contents("output.json");

var_dump($data);

echo $data;

print_r($data);