how to retrieve json data in php code example

Example 1: php return json

header('Content-type: application/json');
echo json_encode($array);

Example 2: how to receive json data in php

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data;

Example 3: fetch value from json link in php

$ch = curl_init();
// IMPORTANT: the below line is a security risk, read https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software
// in most cases, you should set it to true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'url_here');
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);
echo $obj->access_token;

Example 4: how to get data from json array in php

$data = json_decode($json);

Example 5: return json in php

//code igniter
$query="qry";
$query = $this->db->query($query);
$res=$query->result();
return json_encode($res);