Convert PHP data to JSON API url - Mostlikers

18 August, 2015

Convert PHP data to JSON API url

Today i have discussed about Convert php result array to json api url. JSON is a light-weight data-interchange format. Current trend to develop the api and web app server side exchanging data between their servers  XML or JSON format. JSON is the easiest way to translate a JavaScript object into a PHP associative array.

Convert PHP data to JSON API url



Live Demo               Download



JSON

JSON can also be used for web service payloads et al, but it is really convenient for AJAX results. The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML.

Server Responsibilities

Servers MUST send all JSON API data in response documents with the header. So have write header accept type Content-type: application/json; charset=utf-8 code.

PHP Code to JSON

<?php
    header("Content-Type: application/json");    
    $db=new mysqli('localhost','root','','mostlikers'); 
    $sql=$db->query("SELECT * FROM demos ORDER BY lastupdate DESC"); 
    $json_data['my_demos']=array();
    foreach($sql as $rec)//foreach loop  
    {  
        $json_array['name']=$rec['name'];  
        $json_array['link']=$rec['notes']; 
        array_push($json_data['my_demos'],$json_array);
    }
    $json = json_encode($json_data,JSON_PRETTY_PRINT);
    echo isset($_GET['callback'])? "{$_GET['callback']}($json)": $json;
?>

JSONP

PHP code converted to JSONP. if you use JSON data in same domain it will work. Other domain it won't work. This because of Same origin policy, security concept that disallow such operations.

$.getJSON("http://mostlikers.2my4edge.com/php_json/index.php", function(data) { });

it actually works, with IE but it gives security notification and does not work in Chrome at all browser. But JSONP Will support all applications.

Just add ?callback  code with your specify JSON url.

Ex : http://mostlikers.2my4edge.com/php_json/index.php?callback=mostlikers 

 Similar way you can pass the value to server side.

Ex :  http://mostlikers.2my4edge.com/php_json/index.php?name=karthick&callback=mostlikers 

 This code is very simple most of the api and web application developer using like that way only. Front side they will use java script based platform. Server side GET and POST they will use JSONP  

Thank you for visiting. if have any queries write your comments below.

"My message, especially to young people is to have courage to think differently, courage to invent, to travel the unexplored path, courage to discover the impossible and to conquer the problems and succeed. These are great qualities that they must work towards. This is my message to the young people. "
                                             
                                         - A. P. J. Abdul Kalam








Related Topics



1 comment:

  1. Vickey Khachru30 May 2016 at 10:34

    Hey karthick, excellent tutorial you have covered over here, actually I was looking for the same...
    Thanks for the tutorial,

    BTW are you a full time blogger, means are you making your living from home?

    ReplyDelete