PHP code example of thingston / psr7

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

    

thingston / psr7 example snippets




use Thingston\Psr7\Request;

$request = new Request(
    method: 'POST',
    uri: 'https://api.example.com/users?active=1',
    headers: [
        'Content-Type' => 'application/json',
        'Accept' => 'application/json',
    ],
);

$request = $request->withBody(new \Thingston\Psr7\Stream('{"name":"Ada"}'));

echo $request->getMethod();          // POST
echo $request->getRequestTarget();   // /users?active=1
echo $request->getHeaderLine('Host'); // api.example.com



use Thingston\Psr7\Response;
use Thingston\Psr7\Stream;

$response = new Response(
    statusCode: 201,
    headers: ['Content-Type' => 'application/json'],
    body: new Stream('{"created":true}')
);

echo $response->getStatusCode();   // 201
echo $response->getReasonPhrase(); // Created



use Thingston\Psr7\Uri;

$uri = (new Uri('https://example.com/base/path'))
    ->withQuery('page=2')
    ->withFragment('details');

echo (string) $uri; // https://example.com/base/path?page=2#details



use Thingston\Psr7\ServerRequest;

$request = ServerRequest::fromGlobals();

echo $request->getMethod();
echo (string) $request->getUri();
print_r($request->getQueryParams());
print_r($request->getUploadedFiles());



use Thingston\Psr7\UploadedFile;

$file = new UploadedFile('/tmp/report.csv', 1234, UPLOAD_ERR_OK, 'report.csv', 'text/csv');
$file->moveTo(__DIR__ . '/storage/report.csv');