PHP code example of razonyang / php-jsend

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

    

razonyang / php-jsend example snippets


// generates resposne payload.
$payload = PayloadFactory::success($data); // success payload.
// $payload = PayloadFactory::fail($data); // fail payload.
// $payload = PayloadFactory::error($message, $code, $data); // error payload, the code and data are optional.

// if the factory is not suite for your case, creates payload instance directly.
// $payload = (new Payload())->setStatus($status)->setData($data);

// sends response.
echo $payload->toString($options); // the options the second parameter of json_encode, default to JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES

// the payload can also be formatted like this(same effect as above):
// echo json_encode($payload->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);