Let's begin with making ready the environment to begin our programming with PHP for JSON. A common use of JSON is to examine data from a web server and display the data on a web page.
This chapter will train you on the way to exchange JSON data among the client and a PHP server.
As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.
Function | Libraries |
json_encode | Returns the JSON representation of a value. |
json_decode | Decodes a JSON string. |
json_last_error | Returns the last error. |
PHP json_encode() function is made in use for encoding JSON in PHP. This function gives back the JSON representation of a value on success or FALSE on failure.
Syntax
string json_encode ( $value [, $options = 0 ] )
Parameters
Example
How to convert an array into JSON with PHP, Lets examine with the below given example:
<?php $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5); echo json_encode($arr); ?> While carry out, this will generate the below given result: {"a":1,"b":2,"c":3,"d":4,"e":5} How the PHP objects can be transfered into JSON with the below given example: <?php class Emp { public $name = ""; public $hobbies = ""; public $birthdate = ""; } $e = new Emp(); $e->name = "sachin"; $e->hobbies = "sports"; $e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p"); $e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974 12:20:03")); echo json_encode($e); ?>
After applying, it will generate the below given result:
{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}
PHP json_decode() function is made in use for decoding JSON in PHP. This function gives back the value decoded from json to appropriate PHP type.
Syntax
mixed json_decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]])
Parameters
Example
How PHP can be used to decode JSON objects, Let’s examine the below given example:
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); var_dump(json_decode($json, true)); ?> After applying, you will get the below given result: object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }
PHP has few functions that is built to manage JSON.
Objects in PHP can be transformed into JSON by make use of the PHP function json_encode():
PHP File
<?php $myObj->name = "John"; $myObj->age = 30; $myObj->city = "New York"; $myJSON = json_encode($myObj); echo $myJSON; ?>
The Client JavaScript
JavaScript on the client, by making use of an AJAX call to request the PHP file from the instance given below:
Example
const xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() { const myObj = JSON.parse(this.responseText); document.getElementById("demo").innerHTML = myObj.name; } xmlhttp.open("GET", "demo_file.php"); xmlhttp.send();
Arrays in PHP will also be transformed into JSON when making use of the PHP function json_encode():
PHP File
<?php $myArr = array("John", "Mary", "Peter", "Sally"); $myJSON = json_encode($myArr); echo $myJSON; ?>
JavaScript on the client, by making use of an AJAX call to request the PHP file from the array instance given above:
Example
Use JSON.parse() to transform the result into a JavaScript array:
var xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() { const myObj = JSON.parse(this.responseText); document.getElementById("demo").innerHTML = myObj[2]; } xmlhttp.open("GET", "demo_file_array.php", true); xmlhttp.send();
PHP is a server-aspect programming language and may be used to get entry to a database.
Imagine you've got a database for your server, and also you need to send a request to it from the client wherein you ask for the 10 first rows in a table called "customers".
On the client, make a JSON object that describes the numbers of rows you need to return.
Before you send the request to the server, convert the JSON object right into a string and send it as a parameter to the URL of the PHP page:
Example:
Use JSON.stringify() to transform the JavaScript object into JSON:
const limit = {"limit":10}; const dbParam = JSON.stringify(limit); xmlhttp = new XMLHttpRequest(); xmlhttp.onload = function() { document.getElementById("demo").innerHTML = this.responseText; } xmlhttp.open("GET","json_demo_db.php?x=" + dbParam); xmlhttp.send();