PHP code example of bugraozkan / json-php

1. Go to this page and download the library: Download bugraozkan/json-php 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/ */

    

bugraozkan / json-php example snippets





JsonPhp\ApiResponse;

// Create an API response instance
$response = ApiResponse::create();

// Add data to the response
$response->setData([
    'name' => 'Bugra',
    'surname' => 'Ozkan',
    'age' => 21
]);

// Set the HTTP status code
$response->setStatus(200);

// Send the JSON response
$response->send();

$response->send();
// Output: { "name": "Bugra", "surname": "Ozkan", "age": 21 }

$response->sendHeaders(['Content-Type' => 'application/javascript']);
echo "callback(" . json_encode([
    'status' => 200,
    'data' => [
        'name' => 'Bugra',
        'surname' => 'Ozkan',
        'age' => 21
    ]
]) . ");";

$jsonString = json_encode([
    'name' => 'Bugra',
    'surname' => 'Ozkan',
    'age' => 21
]);

// Validate the JSON with an external library or tool

$response->setData([
    'name' => 'Bugra',
    'surname' => 'Ozkan',
    'age' => 21
]);
$response->sendHeaders(['Content-Type' => 'application/json']);
echo json_encode($response->data, JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
bash
composer