PHPackages                             paslandau/guzzle-auto-charset-encoding-subscriber - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. paslandau/guzzle-auto-charset-encoding-subscriber

AbandonedArchivedLibrary[HTTP &amp; Networking](/categories/http)

paslandau/guzzle-auto-charset-encoding-subscriber
=================================================

Plugin for Guzzle 5 to automatically convert the body of a reponse according to a predefined charset.

0.8(10y ago)842MITPHPPHP &gt;=5.5

Since Oct 14Pushed 7y ago1 watchersCompare

[ Source](https://github.com/paslandau/guzzle-auto-charset-encoding-subscriber)[ Packagist](https://packagist.org/packages/paslandau/guzzle-auto-charset-encoding-subscriber)[ RSS](/packages/paslandau-guzzle-auto-charset-encoding-subscriber/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (11)Used By (0)

DEPRECATED ⛔
============

[](#deprecated-)

This repository has been deprecated as of 2019-01-27. That code was written a long time ago and has been unmaintained for several years. Thus, repository will now be [archived](https://github.blog/2017-11-08-archiving-repositories/). If you are interested in taking over ownership, feel free to [contact me](https://www.pascallandau.com/about/).

---

guzzle-auto-charset-encoding-subscriber
=======================================

[](#guzzle-auto-charset-encoding-subscriber)

[![Build Status](https://camo.githubusercontent.com/9c5fc3de84c3a8a6c1a7792b6694a73c5e35b3943a49871978d6f284a49bf535/68747470733a2f2f7472617669732d63692e6f72672f7061736c616e6461752f67757a7a6c652d6175746f2d636861727365742d656e636f64696e672d737562736372696265722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/paslandau/guzzle-auto-charset-encoding-subscriber)

Plugin for [Guzzle 4/5](https://github.com/scripts/guzzle) to automatically convert the body of a reponse according to a predefined charset.

Description
-----------

[](#description)

Getting charsets right is hard. In a perfect world, everybody would use unicode (UTF-8) as character encoding for textual web content but that's just not gonna happen in the near future, so we have to deal with a lot of different encodings in the wild. Unfortunately that's another layer of complexity on top of my application and I really just want it to "work right".

I'm using Guzzle as an underlying library for dealing with HTTP requests and my whole application relies on content beeing encoded in UTF-8. In my locale (Germany) ISO-8859-1 is still widely used and it really messes up the content of an HTTP response, because Guzzle won't automatically convert ISO-8859-1 to my internally used UTF-8. So I decided to write this little plugin to convert any input encoding automatically to another output encoding. Headers and meta tags can be optionally adjusted as well.

### Basic Usage

[](#basic-usage)

```
    $client = new Client();
    $converter = new EncodingConverter("utf-8"); // define desired output encoding
    $sub = new GuzzleAutoCharsetEncodingSubscriber($converter);
    $url = "http://www.myseosolution.de/scripts/encoding-test.php?enc=iso"; // request website with iso-8859-1 encoding
    $req = $client->createRequest("GET", $url);
    $req->getEmitter()->attach($sub);
    $resp = $client->send($req);
```

Requirements
------------

[](#requirements)

- PHP &gt;= 5.5 with [mbstring extension](http://php.net/manual/de/book.mbstring.php)
- Guzzle &gt;= 4.0

Installation
------------

[](#installation)

The recommended way to install guzzle-auto-charset-encoding-subscriber is through [Composer](http://getcomposer.org/).

```
curl -sS https://getcomposer.org/installer | php

```

Next, update your project's composer.json file to include GuzzleAutoCharsetEncodingSubscriber:

```
{
    "repositories": [ { "type": "composer", "url": "http://packages.myseosolution.de/"} ],
    "minimum-stability": "dev",
    "require": {
         "paslandau/guzzle-auto-charset-encoding-subscriber": "dev-master"
    }
    "config": {
        "secure-http": false
    }
}

```

***Caution:** You need to explicitly set `"secure-http": false` in order to access  as repository. This change is required because composer changed the default setting for `secure-http` to true at [the end of february 2016](https://github.com/composer/composer/commit/cb59cf0c85e5b4a4a4d5c6e00f827ac830b54c70#diff-c26d84d5bc3eed1fec6a015a8fc0e0a7L55).*

After installing, you need to require Composer's autoloader:

```
    require 'vendor/autoload.php';
```

Examples
--------

[](#examples)

Let's have a look a the differences between a 'normal' guzzle request and a request with a guzzle-auto-charset-encoding-subscriber at first:

```
    $client = new Client();
    $converter = new EncodingConverter("utf-8",true,true); // define desired output encoding
    $sub = new GuzzleAutoCharsetEncodingSubscriber($converter);
    $url = "http://www.myseosolution.de/scripts/encoding-test.php?enc=iso";

    $tests = [
        "Using unmodified Guzzle request" => null,
        "Using guzzle-auto-charset-encoding-subscriber" => $sub,
    ];
    foreach($tests as $name => $subscriber) {
        $req = $client->createRequest("GET", $url);
        if($subscriber !== null) {
            $req->getEmitter()->attach($sub);
        }
        $resp = $client->send($req);
        echo "    $name\n";
        echo "    Request to $url:\n";
        echo "    Content-Type: " . $resp->getHeader("content-type") . "\n\n";
        echo $resp->getBody()."\n\n";
    }
```

**Output (assuming your editor uses UTF-8 as default)**

```
Using unmodified Guzzle request
Request to http://www.myseosolution.de/scripts/encoding-test.php?enc=iso:
Content-Type: text/html; charset=iso-8859-1; someOtherRandom="header in here"

        Umlauts everywhere �������

        �������

Using guzzle-auto-charset-encoding-subscriber
Request to http://www.myseosolution.de/scripts/encoding-test.php?enc=iso:
Content-Type: text/html; charset=utf-8; someOtherRandom="header in here"

        Umlauts everywhere öäüßÖÄÜ

        öäüßÖÄÜ

```

The requested website delivers content in the ISO-8859-1 encoding. The unmodified guzzle request passes exactly what it gets from the website back to us. If we're expecting UTF-8 encoded content, we will get the "garbage" result shown above, since the german umlauts won't be recognized. Using the guzzle-auto-charset-encoding-subscriber will convert the result from the encoding it finds in either the `content-type` header or the websites `meta` tags. To minimize compatibility issues on subsequent components, the plugin also adjusted the `content-type` header and the `` tag to UTF-8.

The behaviour of the plugin can be modified as follows:

### Adjust the `content-type` header

[](#adjust-the-content-type-header)

By default, the `content-type` header is adjusted when the guzzle-auto-charset-encoding-subscriber converts the body of a request into another encoding. You can prevent this behaviour by setting the `$replaceHeaders` parameter to `false`:

```
    $client = new Client();
    $replaceHeaders = false; // prevent the replacement of the content-type header
    $converter = new EncodingConverter("utf-8",$replaceHeaders);
    $sub = new GuzzleAutoCharsetEncodingSubscriber($converter);
    $url = "http://www.myseosolution.de/scripts/encoding-test.php?enc=iso";
    $req = $client->createRequest("GET", $url);
    $req->getEmitter()->attach($sub);
    $resp = $client->send($req);
```

### Adjust the `meta` tags

[](#adjust-the-meta-tags)

By default, the content of a document is *not* modified (apart from being converted into another encoding). You can explicitly force the guzzle-auto-charset-encoding-subscriber to adjust the `meta` tags within a document to reflect the new encoding by setting the `$replaceContent` parameter to `true`:

```
    $client = new Client();
    $replaceHeaders = null; // default
    $replaceContent = true; // force the replacement of the meta tags within the content
    $converter = new EncodingConverter("utf-8",$replaceHeaders, $replaceContent);
    $sub = new GuzzleAutoCharsetEncodingSubscriber($converter);
    $url = "http://www.myseosolution.de/scripts/encoding-test.php?enc=iso";
    $req = $client->createRequest("GET", $url);
    $req->getEmitter()->attach($sub);
    $resp = $client->send($req);
```

Currently, 3 different cases are handled/recognized:

- HTML 4 (uses ``)
- HTML 5 (uses ``)
- XML (uses ``)

### Forcing a default input encoding

[](#forcing-a-default-input-encoding)

Some websites use no (or wrong) values for the `content-type` header or the `meta` tags. In those cases, the guzzle-auto-charset-encoding-subscriber can be configured to assume a default encoding:

```
    $client = new Client();
    $replaceHeaders = null; // default
    $replaceContent = null; // default
    $fixedInputEncoding = "iso-8859-1"; // assume "iso-8859-1" as default encoding
    $converter = new EncodingConverter("utf-8",$replaceHeaders, $replaceContent,$fixedInputEncoding);
    $sub = new GuzzleAutoCharsetEncodingSubscriber($converter);
    $url = "http://www.myseosolution.de/scripts/encoding-test.php?enc=iso&header=false&meta=false"; // hide charset from header and meta tags
    $req = $client->createRequest("GET", $url);
    $req->getEmitter()->attach($sub);
    $resp = $client->send($req);
```

Related plugins
---------------

[](#related-plugins)

- [guzzle4-charset-subscriber](https://github.com/sasezaki/guzzle4-charset-subscriber) \[Guzzle 4\]
- [guzzle-plugin-AutoCharsetEncodingPlugin](https://github.com/diggin/guzzle-plugin-AutoCharsetEncodingPlugin) \[Guzzle 3\]
- [ForceCharsetPlugin](https://gist.github.com/pschultz/6554265) \[Guzzle 3\]

Frequently searched questions
-----------------------------

[](#frequently-searched-questions)

- How to change the reponse charset/encoding in Guzzle?
- How to convert the charset/encoding of an response in Guzzle?
- How to force an input/output encoding/charset in Guzzle?
- Guzzle responses appear malformed due to encoding/charset - what to do?

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~47 days

Total

10

Last Release

3854d ago

PHP version history (3 changes)0.0.1PHP ~5.5

0.2.0PHP ~5.5|~7

0.7.5PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/295524?v=4)[Benjamin](/maintainers/thebennos)[@thebennos](https://github.com/thebennos)

---

Top Contributors

[![paslandau](https://avatars.githubusercontent.com/u/7747310?v=4)](https://github.com/paslandau "paslandau (18 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/paslandau-guzzle-auto-charset-encoding-subscriber/health.svg)

```
[![Health](https://phpackages.com/badges/paslandau-guzzle-auto-charset-encoding-subscriber/health.svg)](https://phpackages.com/packages/paslandau-guzzle-auto-charset-encoding-subscriber)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.2k532.1M2.5k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k496.1k34](/packages/neuron-core-neuron-ai)[illuminate/http

The Illuminate Http package.

11937.2M6.6k](/packages/illuminate-http)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3661.2M46](/packages/tencentcloud-tencentcloud-sdk-php)[dreamfactory/df-core

DreamFactory(tm) Core Components

1652.0k38](/packages/dreamfactory-df-core)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

252.5k](/packages/eslazarev-wildberries-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
