PHP code example of nimbly / shuttle

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

    

nimbly / shuttle example snippets


$http_factory = new GuzzleHttp\Psr7\HttpFactory;

$shuttle = new Shuttle(
	requestFactory: $http_factory,
	responseFactory: $http_factory,
	streamFactory: $http_factory,
	uriFactory: $http_factory,
);

use Nimbly\Shuttle\Shuttle;

$shuttle = new Shuttle;

$response = $shuttle->get("https://www.google.com");
$response = $shuttle->post("https://example.com/search", "Form data"));

$response = $shuttle->request("connect", "https://api.example.com/v1/books");

$response = $shuttle->get("https://api.example.com/v1/books");

echo $response->getStatusCode(); // 200
echo $response->getReasonPhrase(); // OK

$body = $response->getBody()->getContents(); // {"title": "Do Androids Dream of Electric Sheep?", "author": "Philip K. Dick"}

// Build Request message with your favorite PSR-7 library.
$request = new Request("get", "https://www.example.com");

// Send the Request.
$shuttle = new Shuttle;
$response = $shuttle->sendRequest($request);

use Nimbly\Shuttle\Body\JsonBody;

$book = [
    "title" => "Breakfast Of Champions",
    "author" => "Kurt Vonnegut",
];

$shuttle->post("https://api.example.com/v1/books", new JsonBody($book));

class AuthMiddleware implements MiddlewareInterface
{
	public function __construct(
		private string $api_key)
	{
	}

	public function process(RequestInterface $request, callable $next): ResponseInterface
	{
		// Add the Authorization header with every outgoing request.
		$request = $request->withAddedHeader("Authorization", "Bearer " . $this->api_key);

		// Pass request object to next middleware layer.
		$response = $next($request);

		// Return response back with custom header added.
		return $response->withAddedHeader("X-Custom-Header", "Foo");
	}
}

$shuttle = new Shuttle(
	middleware: [
		new AuthMiddleware(\getenv("API_KEY")),
		new FooMiddleware,
		new BazMiddleware,
	]
);