PHP code example of webignition / guzzle-http-authentication-middleware
1. Go to this page and download the library: Download webignition/guzzle-http-authentication-middleware 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/ */
webignition / guzzle-http-authentication-middleware example snippets
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use webignition\Guzzle\Middleware\HttpAuthentication\AuthorizationType;
use webignition\Guzzle\Middleware\HttpAuthentication\AuthorizationHeader;
use webignition\Guzzle\Middleware\HttpAuthentication\CredentialsFactory;
use webignition\Guzzle\Middleware\HttpAuthentication\HttpAuthenticationMiddleware;
// Creating a client that uses the middleware
$httpAuthenticationMiddleware = new HttpAuthenticationMiddleware();
$handlerStack = HandlerStack::create();
$handlerStack->push($httpAuthenticationMiddleware, 'http-auth');
$client = new Client([
'handler' => $handlerStack,
]);
// Setting credentials on the middleware
$basicCredentials = CredentialsFactory::createBasicCredentials('username', 'password');
$httpAuthenticationMiddleware->setType(AuthorizationType::BASIC);
$httpAuthenticationMiddleware->setCredentials($credentials);
$httpAuthenticationMiddleware->setHost('example.com');
// All requests to example.com (or *.example.com) will now have
// a correct Authorization header set for basic HTTP authentication