A Guzzle middleware to keep track of redirects

Overview

A Guzzle middleware to keep track of redirects

Latest Version on Packagist Tests GitHub Code Style Action Status Total Downloads

This package contains middleware for Guzzle that allows you to track redirects that happened during a request.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/guzzle-redirect-history-middleware

Usage

Here is a quick example of how you can use the Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistoryMiddleware to store the redirect history in a Spatie\GuzzleRedirectHistoryMiddleware\RedirectHistory instance.

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();

$redirects is an array of which item is an array with these keys:

  • status: the status code of the response
  • url: the URL of the performed request that resulted in a redirect

So if you make a request to https://example.com/page-a which redirects to /page-b which finally redirects to /page-c, this will be the content of $redirects

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

Even if your initial request results in a \GuzzleHttp\Exception\TooManyRedirectsException, the RedirectHistory will still contain the performed redirects

Why we created this package

Guzzle has built-in support for tracking redirects. Unfortunately, it isn't that developer friendly to use. You'll have to manipulate and combine arrays found in the X-Guzzle-Redirect-History and X-Guzzle-Redirect-Status-History headers.

Also, when hitting an exception such as TooManyRedirectsException, these headers won't be filled.

Our package makes it easy to retrieve the redirects in a sane format. You're also able to get the redirect history even if the request ultimately fails.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
A package to keep track of your pages & understand your audience
A package to keep track of your pages & understand your audience

A clean way to track your pages & understand your user's behavior Installation You can install the package via composer: composer require coderflexx/l

Laravel Seeable - Keep track of the date and time a user was last seen.

Laravel Seeable This package makes it easy to keep track of the date and time a user was last seen. Installation Install this package. composer requir

PSR-7 middleware foundation for building and dispatching middleware pipelines

laminas-stratigility From "Strata", Latin for "layer", and "agility". This package supersedes and replaces phly/conduit. Stratigility is a port of Sen

Laravel based API to shorten URLs and share them easily. Redirects to the real URL by entering a short URL generated by the API

URL Shortener Requirements: PHP 7.4 or above composer node / npm Installation clone the project from the Github repository, enter the project folder,

Manage redirects using database rules. Rules are intended to be very similar to Laravel default routes, so syntax is pretty easy to comprehend.

Laravel DB redirector Manage HTTP redirections in Laravel using database Manage redirects using database rules. Rules are intended to be very similar

A simple way to add 301/302 redirects within CraftCMS.
A simple way to add 301/302 redirects within CraftCMS.

Redirector plugin for Craft CMS 3.x A simple way to add 301/302 redirects within CraftCMS. This is the first CraftCMS plugin written by myself so plea

Redirects any user which hasn't setup two factor authentication yet to /2fa/

force-two-factor Redirects any user which hasn't setup two factor authentication yet to /2fa/. Use together with the forked two-factor plugin at https

Simply removes the applcation's front-end and redirects it to the admin area.

Simply removes the application's front-end and redirects it to the admin area.

Create custom WordPress routes and redirects, restrict access by roles and/or capabilities. Routes made simple
Create custom WordPress routes and redirects, restrict access by roles and/or capabilities. Routes made simple

Create custom WordPress routes and redirects, restrict access by roles and/or capabilities. Routes made simple

 Create and Control Page Redirects through Filament
Create and Control Page Redirects through Filament

Create and Control Page Redirects through Filament A Filament resource to create and maintain page redirects for your website. Installation You can in

Laravel package for manage your URL redirects in database or other sources to get better SEO results

Laravel 8 and 9 package to manage URL redirections inside your Laravel application using different data sources. It allows a better SEO support for your Laravel site.

A Laravel URL Shortener package that provides URL redirects with optionally protected URL password, URL expiration, open limits before expiration

A Laravel URL Shortener package that provides URL redirects with optionally protected URL password, URL expiration, open limits before expiration, ability to set feature activation dates, and click tracking out of the box for your Laravel applications.

Guzzle, an extensible PHP HTTP client
Guzzle, an extensible PHP HTTP client

Guzzle, PHP HTTP client Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interf

Supercharge your app or SDK with a testing library specifically for Guzzle

Full Documentation at guzzler.dev Supercharge your app or SDK with a testing library specifically for Guzzle. Guzzler covers the process of setting up

Simple handler system used to power clients and servers in PHP (this project is no longer used in Guzzle 6+)

RingPHP Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function. RingPHP be used to power HTTP clie

A simple API with Guzzle wrapper, providing easy access to wppconnect's endpoints.

WPPConnect Team Wppconnect Laravel Client A simple API with Guzzle wrapper, providing easy access to wppconnect's endpoints. Requirements PHP 7.4 or n

Provide a module to industrialize API REST call with dependency injection using Guzzle library
Provide a module to industrialize API REST call with dependency injection using Guzzle library

Zepgram Rest Technical module to industrialize API REST call with dependency injection using Guzzle library. Provides multiple features to make your l

This plugin integrates cache functionality into Guzzle Bundle, a bundle for building RESTful web service clients.

Guzzle Bundle Cache Plugin This plugin integrates cache functionality into Guzzle Bundle, a bundle for building RESTful web service clients. Requireme

PHP library/SDK for Crypto APIs 2.0 using Guzzle version 7

cryptoapis/sdk-guzzle7 Crypto APIs 2.0 is a complex and innovative infrastructure layer that radically simplifies the development of any Blockchain an

Comments
  • Add reason and headers

    Add reason and headers

    When reading the redirect chain I needed more details such as headers.

    Since Guzzle already manages the reason (so as to explain the status code) and the headers of each request, I made a small change to the middleware to add this information.

    They could be useful to others, they are not blockers, I thought about doing the PR.

    opened by bessone 7
Releases(1.0.1)
Owner
Spatie
We create open source, digital products and courses for the developer community
Spatie
This plugin integrates OAuth2 functionality into Guzzle Bundle

Guzzle Bundle OAuth2 Plugin This plugin integrates OAuth2 functionality into Guzzle Bundle, a bundle for building RESTful web service clients. Prerequ

Vlad Gregurco 12 Oct 30, 2022
this is a semester project using Laravel, this app allow user to keep and shear their note with other users.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Vichhagar Nhin 0 Dec 24, 2021
PSR-7 and PSR-15 JWT Authentication Middleware

PSR-7 and PSR-15 JWT Authentication Middleware This middleware implements JSON Web Token Authentication. It was originally developed for Slim but can

Mika Tuupola 782 Dec 18, 2022
PSR-7 and PSR-15 HTTP Basic Authentication Middleware

PSR-7 and PSR-15 Basic Auth Middleware This middleware implements HTTP Basic Authentication. It was originally developed for Slim but can be used with

Mika Tuupola 430 Dec 30, 2022
Middleware to generate access logs for each request using the Apache's access log format

Middleware to generate access logs for each request using the Apache's access log format. This middleware requires a Psr log implementation, for example monolog.

Middlewares 20 Jun 23, 2022
Routes and Middleware for Using OAuth2 Server within a Slim Framework API

Chadicus\Slim\OAuth2 A collection of OAuth2 Server routes, middleware and utilities for use within a Slim 3 Framework API Requirements Chadicus\Slim\O

Chad Gray 126 Oct 8, 2022
A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.

A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.

Kevin Robatel 371 Dec 17, 2022
DaybydayCRM an open-source CRM, to help you keep track of your daily workflow.

====================== DaybydayCRM is an everyday customer relationship management system (CRM) to help you keep track of your customers, tasks, appoi

Casper Bottelet 2.1k Dec 30, 2022
DaybydayCRM an open-source CRM, to help you keep track of your daily workflow.

====================== DaybydayCRM is an everyday customer relationship management system (CRM) to help you keep track of your customers, tasks, appoi

Casper Bottelet 2.1k Jan 3, 2023
A package to keep track of outgoing emails in your Laravel application.

Keep track of outgoing emails and associate sent emails with Eloquent models This package helps you to keep track of outgoing emails in your Laravel a

Stefan Zweifel 108 Nov 1, 2022