PHP code example of robbie / psr7-adapters

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

    

robbie / psr7-adapters example snippets




$myResponse = new \SilverStripe\Control\HTTPResponse(
    json_encode(['success' => true, 'message' => 'Your request was successful!']),
    200,
    'OK'
);

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = (new \Robbie\Psr7\HttpResponseAdapter)->toPsr7($myResponse);



$newResponse = $response->withHeader('Content-Type', 'application/json');
$newResponse = $newResponse->withHeader('X-Custom-Header', 'my-value-here');

// $response !== $newResponse -> #psr7-ftw



# Context: PageController
use Robbie\Psr7\HttpRequestAdapter;

// ...

$request = $this->getRequest();
$adapter = new HttpRequestAdapter;
$psrInterface = $adapter->toPsr7($request);

// Outputs all your initial request headers:
print_r($psrInterface->getHeaders());



// $requestInterface is an instance of Psr\Http\Message\ServerRequestInterface
$httpRequest = (new HttpRequestAdapter)->fromPsr7($requestInterface);