1. Go to this page and download the library: Download yiisoft/data-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/ */
yiisoft / data-response example snippets
use Yiisoft\DataResponse\ResponseFactory\JsonResponseFactory;
use Yiisoft\DataResponse\Formatter\JsonFormatter;
/**
* @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
*/
$factory = new JsonResponseFactory($responseFactory, new JsonFormatter());
$response = $factory->createResponse(['key' => 'value']);
$response->getBody()->rewind();
echo $response->getBody()->getContents(); // {"key":"value"}
echo $response->getHeaderLine('Content-Type'); // application/json; charset=UTF-8
use Yiisoft\DataResponse\Middleware\JsonDataResponseMiddleware;
use Yiisoft\DataResponse\Formatter\JsonFormatter;
$middleware = new JsonDataResponseMiddleware(new JsonFormatter());
use Yiisoft\DataResponse\Formatter\HtmlFormatter;
use Yiisoft\DataResponse\Formatter\XmlFormatter;
use Yiisoft\DataResponse\Formatter\JsonFormatter;
use Yiisoft\DataResponse\Middleware\ContentNegotiatorDataResponseMiddleware;
$middleware = new ContentNegotiatorDataResponseMiddleware(
formatters: [
'text/html' => new HtmlFormatter(),
'application/xml' => new XmlFormatter(),
'application/json' => new JsonFormatter(),
],
fallback: new JsonFormatter(),
);
use Yiisoft\DataResponse\ResponseFactory\ContentNegotiatorResponseFactory;
use Yiisoft\DataResponse\ResponseFactory\JsonResponseFactory;
use Yiisoft\DataResponse\ResponseFactory\XmlResponseFactory;
/**
* @var JsonResponseFactory $jsonResponseFactory
* @var XmlResponseFactory $xmlResponseFactory
*/
$factory = new ContentNegotiatorResponseFactory(
factories: [
'application/json' => $jsonResponseFactory,
'application/xml' => $xmlResponseFactory,
],
fallback: $jsonResponseFactory,
);
$response = $factory->createResponse($request, ['key' => 'value']);
use Yiisoft\DataResponse\DataStream\DataStream;
use Yiisoft\DataResponse\Formatter\JsonFormatter;
use Yiisoft\DataResponse\Formatter\XmlFormatter;
$stream = new DataStream(['key' => 'value'], new JsonFormatter());
echo (string) $stream; // {"key":"value"}
// You can change the data or formatter dynamically
$stream->changeData(['new' => 'data']);
$stream->changeFormatter(new XmlFormatter());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.