1. Go to this page and download the library: Download ket-php/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/ */
ket-php / http-response example snippets
use KetPHP\Http\Common\Payload\JsonPayload;
$payload = (new JsonPayload())
->with('message', 'User created successfully')
->with('user', [
'id' => 1,
'created_at' => new DateTimeImmutable(),
]);
// Convert to array
$arrayData = $payload->jsonSerialize();
// Serialize to JSON string
$jsonString = $payload->toJson();
// or simply cast to string: $jsonString = (string) $payload;
use KetPHP\Http\HttpResponse;
use KetPHP\Http\Common\Payload\JsonPayload;
use Psr\Http\Message\ResponseInterface;
/** @var ResponseInterface $psrResponse */
// Using default JsonPayload
$response = HttpResponse::create()
->with('status', 'success')
->with('data', $userData)
->toResponse($psrResponse);
// Overriding HTTP status code directly on PSR-7 response
$response = HttpResponse::create()
->with('error', 'Resource Not Found')
->toResponse($psrResponse)
->withStatus(404);
use KetPHP\Http\Common\Support\PayloadTrait;
use KetPHP\Http\Common\PayloadInterface;
final class XmlPayload implements PayloadInterface
{
use PayloadTrait;
private array $payload = [];
public function setPayload(array $payload): static
{
$this->payload = $this->prepareData($payload);
return $this;
}
public function with(string $key, mixed $data): static
{
$this->payload[$key] = $this->prepareData($data);
return $this;
}
public function getContentType(): string
{
return 'application/xml';
}
public function jsonSerialize(): array
{
return $this->payload;
}
public function __toString(): string
{
// Custom XML rendering implementation
return '<xml>...</xml>';
}
// and etc...
}