how to return & receive json data from web api in php code example
Example 1: fetch value from json link in php
$ch = curl_init();
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 2: web api return json example in php
$option = $_GET['option'];
if ( $option == 1 ) {
$data = [ 'a', 'b', 'c' ];
} else {
$data = [ 'name' => 'God', 'age' => -1 ];
}
header('Content-type: application/json');
echo json_encode( $data );