PHP code example of duffleman / json-client

1. Go to this page and download the library: Download duffleman/json-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

duffleman / json-client example snippets




uffleman\JSONClient\JSONClient;

$client = new JSONClient('http://jsonplaceholder.typicode.com/');

try {
    $response = $client->request('GET', 'users');

    foreach ($response as $user) {
        echo $user->name."\n";
        echo $user->address->geo->lat."\n";
        echo $user->company->name."\n";
        echo "\n";
    }
} catch (Exception $error) {
    dump($error);
}



uffleman\JSONClient\JSONClient;

// important trailing slash
$client = new JSONClient('https://api.avocado.cuv-nonprod.app/1/service-staff/');

try {
    $response = $client->request('POST', '1/latest/list_staff_public');

    foreach ($response as $staff) {
        echo $staff->about->name.' ('.$staff->about->role.')';
        echo "\n";
    }
} catch (Exception $error) {
    dump($error);
}



use function Duffleman\JSONClient\encode;

wards, we can use it as a normal function :)

$my_data = [
    'name' => 'James',
    'age' => 21,
    'admin' => true,
];

$json = encode($my_data);

echo $json;