PHP code example of vaibhavpandeyvpz / sandesh

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\UriFactory;

$uri = (new UriFactory())
    ->createUri('https://user:[email protected]:8080/path?query=value#fragment');

echo $uri->getScheme();    // 'https'
echo $uri->getHost();      // 'example.com'
echo $uri->getPort();      // 8080
echo $uri->getPath();      // '/path'
echo $uri->getQuery();     // 'query=value'
echo $uri->getFragment();  // 'fragment'



use Sandesh\StreamFactory;

// Create empty stream
$stream = (new StreamFactory())->createStream();
$stream->write('Hello, World!');

// Create from file
$stream = (new StreamFactory())
    ->createStreamFromFile('/path/to/file.txt', 'r');

// Create from resource
$resource = fopen('php://input', 'r');
$stream = (new StreamFactory())
    ->createStreamFromResource($resource);

// Read stream
$content = $stream->getContents();
$stream->rewind(); // Reset pointer



use Sandesh\UploadedFileFactory;
use Sandesh\StreamFactory;

$streamFactory = new StreamFactory();
$fileFactory = new UploadedFileFactory();

$stream = $streamFactory->createStreamFromFile($_FILES['avatar']['tmp_name']);

$uploadedFile = $fileFactory->createUploadedFile(
    $stream,
    $_FILES['avatar']['size'],
    $_FILES['avatar']['error'],
    $_FILES['avatar']['name'],
    $_FILES['avatar']['type']
);

// Move uploaded file
$uploadedFile->moveTo('/path/to/destination.jpg');



use Sandesh\CookieFactory;

// Parse Set-Cookie header
$cookie = (new CookieFactory())
    ->createCookie('session=abc123; Domain=.example.com; Path=/; HttpOnly; Secure; Max-Age=3600');

// Access cookie properties
echo $cookie->getName();     // 'session'
echo $cookie->getValue();    // 'abc123'
echo $cookie->getDomain();   // '.example.com'
echo $cookie->getPath();     // '/'
echo $cookie->isHttpOnly();  // true
echo $cookie->isSecure();    // true

// Convert back to Set-Cookie header string
$header = (string) $cookie;



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



$request = $request->withAttribute('route', 'users.show')
    ->withAttribute('user', $user);

$route = $request->getAttribute('route');
$user = $request->getAttribute('user');