PHP code example of gridonic / json-response

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

    

gridonic / json-response example snippets




/**
 * @throws \InvalidArgumentException
 */
 new SuccessJsonResponse($data, 'Success message', 'Success title', 200);

/**
 * @throws \InvalidArgumentException
 */
 new ErrorJsonResponse($data, 'Error message', 'Error title', 400, 'e311', $errors);

new SuccessJsonResponse();

$data = array(
    'post' => array(
        'id' => 1,
        'title' => 'A blog post',
    )
);
$message = 'The Blog post was successfully created.';
$title = 'Successfully created!';
$statusCode = 205;

new SuccessJsonResponse($data, $message, $title, $statusCode);

$message = 'Oups, data is missing.';

new ErrorJsonResponse(null, $message); // you have to send a message!

$data = array(
    'post' => array(
        'title' => 'A blog post',
    )
);
$message = 'Oups, data is not correct.';
$title = 'An error occured!';
$statusCode = 400;
$errorCode = e311;
$errors = array(
    'body' => 'The parameter is missing.',
    'title' => 'This parameter is too long.'
);

new ErrorJsonResponse($data, $message, $title, $statusCode, $errorCode, $errors);