PHP code example of spatie / guzzle-redirect-history-middleware

1. Go to this page and download the library: Download spatie/guzzle-redirect-history-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/ */

    

spatie / guzzle-redirect-history-middleware example snippets


use Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistory;
use Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistoryMiddleware;

/*
 * First create a new instance of `RedirectHistory`
 * This instance can be used after the requests to get the redirects.
 */
$redirectHistory = new RedirectHistory();

/*
 * This is the default way to add a middleware to Guzzle
 * default middleware stack.
 */
$stack = HandlerStack::create();
$stack->push(RedirectHistoryMiddleware::make($redirectHistory));

/*
 * Let's create Guzzle client that uses the middleware stack
 * containing our `RedirectHistoryMiddleware`.
 */
$client = new Client([
    'handler' => $stack,
]);

/*
 * Now, let's make a request.
 */
$response = $client->get($anyUrl);

/*
 * And tada, here are all the redirects performed
 * during the request.
 */
$redirects = $redirectHistory->toArray();

[
    ['status' => 302, 'url' => 'https://example.com/page-a'],
    ['status' => 302, 'url' => 'https://example.com/page-b'],
    ['status' => 200, 'url' => 'https://example.com/page-c'],
];