PHP code example of lkt / http-response

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

    

lkt / http-response example snippets


use Lkt\Http\Response;

Response::status(200, ['some' => 'data']);

use Lkt\Http\Response;

Response::ok(['some' => 'data']); // Same as: Response::status(200, ['some' => 'data']);

use Lkt\Http\Response;

$response = Response::ok(['hey' => 'how are you?']);

// Set content type to JSON (default Content Type)
$response->setContentTypeJSON();

// Or to text/html
$response->setContentTypeTextHTML();

// Also, you can set the expiration and max age:
$response->setCacheControlMaxAgeHeaderToOneYear(84600);
$response->setExpiresHeader(84600);

// Or use the shortcuts:
$response->setExpiresHeaderToOneDay();
$response->setExpiresHeaderToOneWeek();
$response->setExpiresHeaderToOneMonth();
$response->setExpiresHeaderToOneYear();

$response->setCacheControlMaxAgeHeaderToOneDay();
$response->setCacheControlMaxAgeHeaderToOneWeek();
$response->setCacheControlMaxAgeHeaderToOneMonth();
$response->setCacheControlMaxAgeHeaderToOneYear();

// Send the response
$response->sendContent(); 

use Lkt\Http\Response;
$response = Response::ok('may the force be with you');

// Output content
$response->sendContent();

use Lkt\Http\Response;

// Get a string with the content of the file
$content = file_get_contents($pathToImage);

// Create a response
$response = Response::ok($content);

// Set the MIME type for the file
// Automatically detect the mime type from file extension
// Notice: If the extension wasn't detected, the response will turn into an octet-stream
$response->setContentTypeByFileExtension('jpg'); // It can be pdf, png, doc, docx, csv, ...

// Set the last modified header
$lastModified = filemtime($pathToImage);
$response->setLastModifiedHeader($lastModified);

// Turn it to download
$response->setContentDispositionAttachment('image.jpg');

// Output img content
$response->sendContent();

use Lkt\Http\Response;

return Response::ok(['some' => 'data']);

use Lkt\Http\Response;

return Response::ok('<p>Hello world!</p>');

use Lkt\Http\Response;

return Response::forbidden(); // Empty
return Response::forbidden(['some' => 'data']); // JSON info
return Response::forbidden('Forbidden'); // text/html info

use Lkt\Http\Response;

return Response::ok(file_get_contents($pathToImage))
    ->setContentTypeByFileExtension('jpg')
    ->setLastModifiedHeader(filemtime($pathToImage));

use Lkt\Http\Response;

return Response::ok(file_get_contents($pathToImage))
    ->setContentTypeByFileExtension('jpg')
    ->setContentDispositionAttachment('image.jpg')
    ->setLastModifiedHeader(filemtime($pathToImage));