1. Go to this page and download the library: Download vaibhavpandeyvpz/sandesh 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/ */
vaibhavpandeyvpz / sandesh example snippets
use Sandesh\RequestFactory;
use Sandesh\Request;
// Using factory
$request = (new RequestFactory())
->createRequest('POST', 'https://api.example.com/users')
->withHeader('Content-Type', 'application/json')
->withBody($stream);
// Direct instantiation
$request = new Request('GET', 'https://example.com');
use Sandesh\ServerRequestFactory;
// Create from server superglobal
$request = (new ServerRequestFactory())
->createServerRequest('POST', 'https://example.com/api', $_SERVER)
->withParsedBody(['name' => 'John'])
->withQueryParams(['page' => 1])
->withCookieParams($_COOKIE)
->withUploadedFiles($uploadedFiles);
use Sandesh\ResponseFactory;
use Sandesh\Response;
// Using factory
$response = (new ResponseFactory())
->createResponse(200)
->withHeader('Content-Type', 'application/json')
->withBody($stream);
// Direct instantiation with body
$response = new Response(404, 'Not Found', 'Page not found');
use Sandesh\ServerResponseSender;
use Sandesh\ResponseFactory;
$response = (new ResponseFactory())
->createResponse(200)
->withHeader('Content-Type', 'application/json')
->withBody($stream);
$sender = new ServerResponseSender();
$sender->send($response);
use Sandesh\HttpMethod;
// Type-safe HTTP method enum
$method = HttpMethod::POST;
echo $method->toString(); // 'POST'
// Create from string
$method = HttpMethod::fromString('get'); // HttpMethod::GET
// Use in request
$request = new Request($method->toString(), $uri);
$request = new Request('GET', 'https://example.com');
$request2 = $request->withHeader('X-Custom', 'value');
// $request is unchanged
assert($request->hasHeader('X-Custom') === false);
assert($request2->hasHeader('X-Custom') === true);
// JSON body
$request = $serverRequest->withHeader('Content-Type', 'application/json')
->withBody($jsonStream);
$data = $serverRequest->getParsedBody(); // Automatically decoded JSON
// Form data
$request = $serverRequest->withHeader('Content-Type', 'application/x-www-form-urlencoded')
->withBody($formStream);
$data = $serverRequest->getParsedBody(); // Parsed as array
// XML body
$request = $serverRequest->withHeader('Content-Type', 'text/xml')
->withBody($xmlStream);
$data = $serverRequest->getParsedBody(); // SimpleXMLElement instance