PHP code example of guil95 / api-response

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

    

guil95 / api-response example snippets

 
composer 

namespace MyNamespace;

use ApiResponse\Response\ApiResponse;

class MyClass{
  public function save()
  {
    //process save
    
    //Return json
    // $dataResponse: can be, string, array or object (show only public attributes)
    ApiResponse::json([
      'data' => $dataResponse,
      'message' => 'Persist object!'
    ], ApiResponse::HTTP_CREATED);
    
    //Return xml
    /**
     * @param array $data
     * @param int $status
     * @param string|null $root
     */
    ApiResponse::xml([
      'data' => $dataResponse,
      'message' => 'Persist object!'
    ],ApiResponse::HTTP_CREATED,
    'root');
  }
}