get api php json example
Example 1: php json request get value
<?php
$jsonurl = "https://reqres.in/api/users/2";
$json = file_get_contents($jsonurl);
$jsonDecode = json_decode($json, true);
echo $jsonDecode['data']['email'];
?>
Example 2: create curl api request php with para
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "api.example.com");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
Example 3: 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 );
Example 4: create curl api request php with para
$data_array = array(
"amount" => (string)($lease['amount'] / $tenant_count)
);
$update_plan = callAPI('PUT', 'https://api.example.com/put_url/'.$lease['plan_id'], json_encode($data_array));
$response = json_decode($update_plan, true);
$errors = $response['response']['errors'];
$data = $response['response']['data'][0];